Thursday, January 28, 2021
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.1
- 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 Hugo2:
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:3
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.4
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 | > |
-
This all pertains to the macOS flavour of the Unix shell. There are important differences, seemingly in the
datefunction, for example. ↩︎ -
I should know what that format is called, but I don’t. I think it’s ISO 8601. Also, this requires
sd. If you don’t have it, you’d needsedinstead. ↩︎ -
Source - Stack Overflow ↩︎
-
Source - Stack Overflow ↩︎
no way