| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 |
Bash shell script how to add today’s date
Date command is use to print or set the system date and time under
Linux/Unix like operating systems. However some time you need to
include today’s date in shell script. You need to use command
substitution in your shell script to display today’s date. Bash/sh
shell performs the expansion by executing command and replacing the
command substitution with the standard output
of the command, with any trailing newlines deleted. Command
substitution allows the output of a command to replace the command
name. You can use following syntax:
$(command)
OR
`command`
For example type following at shell prompt to display today’s date:
$ echo "Today is $(date)"
Output:
Today is Sat Jan 28 15:48:11 IST 2006
Here is sample script, it stores today’s date in TODAY and hostname in HOST variable, which is used anywhere in script:
#!/bin/bash
TODAY=$(date)
HOST=$(hostname)
echo "-----------------------------------------------------"
echo "Date: $TODAY Host:$HOST"
echo "-----------------------------------------------------"
# add rest of shell script code...
Save above script and execute it. See working shell script that notify admin user if Linux/FreeBSD system load crossed certain limit

Recent comments