time=$(date “+%Y%m%d-%H%M%S”)
or
time=$(date “+%Y-%m-%d %H:%M:%S”)
……
in any format you want
echo “${time}”
the above two lines of simple code is that the shell takes the current time and outputs it in the format it wants.
a couple of things to note
- date is followed by a space, otherwise the command cannot be recognized, the shell is very strict with the space.
- Y shows 4-digit years, such as: 2018; Y displays a 2-digit year, such as: 18. M is for month; M is for minutes. D represents the day, and D represents the current date, i.e. 1/18/18(i.e. 2018.1.18). H represents hours, and H shows months (somewhat muddled). S displays the current second in milliseconds; S displays the current second in seconds.