Tag Archives: Linux Shell programming

The shell gets the current time of the system and formats it

time=$(date “+%Y%m%d-%H%M%S”)

or

time=$(date “+%Y-%m-%d %H:%M:%S”)

… and other formats you want

echo “${time}”

The above two lines of simple code are shell to get the current time and output it in the format you want.

Several points need to be noted

There is a space after

    date, otherwise the command cannot be recognized, and the shell is very strict on spaces. Y shows 4-digit year, such as 2018; y shows 2-digit year, such as 18. M is the month; m is the minute. D is the day, and D is the current date, such as 1 / 18 / 18 (2018.1.18). H is the hour and H is the month. S displays the current second in milliseconds; s displays the current second in seconds.

Shell gets the current time of the system and formats it

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

  1. date is followed by a space, otherwise the command cannot be recognized, the shell is very strict with the space.
  2. 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.