A bunch of Unix date scripting things
Unix shell scripting is not one of my better-know areas of programming, but it’s on my list of things to learn. Some interesting bits about the date
function.
- Want the long date, like Thursday, January 28, 2021? →
date +"%A, %B %d, %Y"
- Want something like the above but with abbreviated month Thursday, Jan 28, 2021? →
date +"%A, %b %d, %Y"
- Time zone in the format of "-05:00" on macOS is:
#!/bin/bash
tz=$(date +"%z" | sd '(\d{2})(\d{2})' '$1:$2')
echo $tz
- Long date in the format of 2021-01-28T05:30:48-05:00 that is use by Hugo:
tz=$(date +"%z" | sd '(\d{2})(\d{2})' '$1:$2')
md_date=`date +"%Y-%m-%dT%H:%M:%S"`
- Speaking of dates, this reference has very detailed information on date and time in the Unix shell.
- Date in the format of 2021-01-28 is
date +"%Y-%m-%d"
Unix slurp command output into a variable
In the UNIX shell, slurping the output of the command into a variable is done this way:
OUTPUT=$(ls -1)
echo "${OUTPUT}"
MULTILINE=$(ls \
-1)
echo "${MULTILINE}"
Append or overwrite a file in Unix shell
You can either append to a file or overwrite the contents from the shell.
To append to a file, it’s echo "hello" >> file.txt
whereas to overwrite the contents of a file, it’s echo "hello" > file.txt
Function |
Symbol |
Append |
>> |
Overwrite |
> |