How to display current date using Shell Script

Shell Script Tutorial for Beginner - Hello everyone, today I will continue to give a short example about shell script and hopefully will be useful for anyone who wants to start learning Shell Script. Today, I am going to show you how to display current date via Shell Script.

First, create an empty file called exercise01.sh. To do it, open Terminal and type the following command

nano exercise01.sh

It will create a new file under current directory.

Now write down the following code

clear
echo "Hello $USER"
echo "Today is \c ";date
exit 0

Now close the file using CTRL + X and make it executable

chmod +x exercise01.sh

Now Run it

sh exercise01.sh

Example:

display date script

Please note that the "\c" on the script will show the date as the same line with text "Today is". Without the "\c", the date will be displayed under the "Today is" line.


EmoticonEmoticon