Q. How can I display or show today's UNIX computer date from a shell script using echo command? How do I store date command output to a variable?
A. 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 the 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, that stores today's date in TODAY and hostname in HOST variable:
#!/bin/bash TODAY=$(date) HOST=$(hostname) echo "-----------------------------------------------------" echo "Date: $TODAY Host:$HOST" echo "-----------------------------------------------------" # add rest code...
Save above script and execute it. See working shell script that notify admin user if Linux/FreeBSD system load crossed certain limit
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 12 comments… read them below or add one }
Thanks a lot!! XD
I have a question in regards to creating a bash shell script using the date command to calculate someones’ age. The user provides the script with the month and year in which they were born, but i am having trouble figuring out how to use the date command with the user input to calculate age. Any info in regards to this matter would be greatly appreciated. Thanks
How to check the pervious date in unix.
for eg today is 25th Feb and i want to print 24th Feb
Ive just used this on a debian (Ubuntu 8.x) server and had to use $DATE=’date’ instead of (date).
Is this because of Debian/Ubuntu or due to me using #!/bin/sh rather than #!/bin/bash as bad habit.
Thanks
Lien
how can i display the date by using shell programming in the UNIX with out using date command ?
sweetness! just helped me add the date command to my backup script!
How to check the pervious date in shell script.
Thank you very much. Really your website is very helpful to me.
Please help. I plan to create the shell scripts that it gives me the date in this format like 2012-01-13. Due i need to move some files from /abc/defg/*.c to /z/abc/2012-01-13. The folder of 2012-01-13 will be change every day and depends on which day I run the scripts. I want to set up as cronjob. Thank in advance
@web-design-dude #!/bin/sh is fine it works in my ubuntu…. what u need to do is declare variable and then assign the value :
$DATE
DATE=(date)
this works perfectly fine
Can you help me with creating a script that reads the system time and if its before noon the it prints a message saying its the morning and its after noon it says its afternoon etc..
Hey John
Try