A self-help tutorial for shell scripting in Linux for beginners, with examples.

Shell is a command interpreter that reads commands from a file or from a keyboard. For programmers, end users, and administrators, shells such as sh, tsh, csh, bash, and others are provided.

Bash (Bourne Again shell), created by Stephen Bourne at Bell Labs, is the most popular and user-friendly shell nowadays.

Shell scripting begins with the shebang command, which instructs the operating system which binary should be used as the script’s interpreter.

The Bash shell’s shebang looks like this:

Any command-line activity that admins or users conduct on a regular basis can be turned into a shell script to make their jobs easier.

Any text editor can be used to write a script.

So here we start it by taking a sample example:

Simply open a text editor and type the following, then save the modifications with the name sample testfile.

The echo command prints “Message written in this quote” on Standard Output, such as a terminal or screen.

“Hello ZNET,” echo

Run/execute the sample script with shell command as:

]# bash testfile root

The # character has been utilized to comment on our sample code. Writing comments for each line or segment of code is a good practice since it helps other admins or users understand what this scriptlet or code will perform.

Linux shell script writers are familiar with the command line and are capable of writing the script.

A script, like any other programming or scripting language, has variables, keywords, data types, conditional statements, instructions, comments, and cases. Although the styles differ, the logics and intellectual flows that underpin them are almost identical.

Another example:

Shell script for Linux administrators who are concerned about server CPU load and want to be notified when it reaches a certain threshold.

Simply write it in any text editor, save your modifications, then use a scheduling task tool like Cron Job to have it run every 2 minutes.


Shell script to check the average CPU load and receive email warnings once the load reaches 10

CPULOAD is a variable that has a constant value. ####


Variable #### LOADAVG is a variable that stores whatever the command’s output is.

The command then reads the specific output and reduces it to a single numeric number.

$(cat /proc/loadavg |awk ‘print $1’ |cut -d”.” -f1) LOADAVG

If conditional statement

It compares and determines the LOADAVG number, and if it is greater than or equal to CPULOAD, it sends an email to the admin at admin@domain.tld.



” $HOSTNAME CPU LOAD ALERT Avg=$LOADAVG ” mail -s admin@domain.tld

That’s it, administrators! To make your work easier, have fun writing scripts.

Please share your thoughts in the comments area.

Leave a Reply

Your email address will not be published. Required fields are marked *