Linux Mint Shell Scripting Primer : Part II

[ This is a five part series that covers the very basics of Linux shell scripting. ]

In the previous part, we had seen the basic structure of a Linux shell script, how to make it executable and then run it.

This part will cover how to output relevant text messages and make the shell scripts more user friendly.

Just to make the script of previous part (which simply displayed the computer name using hostname command) a bit more clearer, let’s output some text before displaying the computer name. This is done with the help of echo.

Example :
echo “ Some text here “. The result would be : Some text here

There are certain nuances when using echo command in shell scripts like when using echo without quotes, we can’t use a semicolon and so on. Make sure to read the man page of echo (man echo in Terminal to get an overall idea).

The above script is modified to include echo as follows :

The output for above will be :

It just outputs the text we want to display for some clarity, that’s all. (It is also useful when trying to get some user inputs or displaying the status message of a script.)

Consider this useful script that prints current date and time, displays who is currently logged in and shows for how long the system has been running :

Terminal commands date, who and uptime are used with some meaningful text to tell you what is really being displayed. Note that we used echo -n to format the output. On running the script (once it is made executable by chmod a+x <script name>) we get :

Pretty handy info to get and that too by only writing the above script once and running it again and again !

Stay tuned for part III for some more fun stuff.