UNIX bash shell scripting – Example tutorial

A BASH shell scripting is a feature that allows you to change the behavior of how the BASH shell script work.

BASH shell scripting

We discuss some BASH shell scripting commands, which help you to control shell scripts.

The echo command is used to display output in the terminal; in the example below, the command will display the value of the TELECOMACADMEY variable.

The TELECOMACADMEY variable defines how many previous commands to store in the history list. To display the value of the variable, use a dollar sign $ character before the variable name. To modify the value of the variable, you don’t use the $ character:

export Command

There are two types of variables used in the BASH shell scripting, local and environment. If you want to view environment variables, use the env command. In this case, the search for variable1 in the environment variables results in no output.

After exporting variable1, it is now an environment variable. Notice that this time, it is found in the search through the environment variables. The export command can also be used to make an environment variable upon its creation, To change the value of an environment variable, simply omit the $ when referencing it:

which Command

You can use which command to display the full path to the  command in, instead of look manually each directory.


An alias can be used to map longer commands to shorter key sequences. When the shell sees an alias being executed, it substitutes the longer sequence before proceeding to interpret commands.

For example, the command ls -l is commonly aliased to lor ll. Because these smaller commands are easier to type, it becomes faster to run the ls -l command line.

You can determine what aliases are set on your shell with the alias command:

The aliases that you see from the previous examples were created by initialization files. These files are designed to make the process of creating aliases automatic.

New aliases can be created by typing alias name=commandwhere name is the name you want to give the alias and command is the command you want to have executed when you run the alias.

For example, you can create an alias so that lh displays a long listing of files, sorted by size with a “human friendly” size with the alias lh=’ls -Shl’ command. Typing lh should now result in the same output as typing the ls -Shlcommand:


Asterisk (*)

The asterisk character is used to represent zero or more of any character in a filename. For example, suppose you want to display all of the files in the /etc directory that begin with the letter A:

Question Mark (?)

The question mark represents any one character. Each question mark character matches exactly one character, no more and no less.

Suppose you want to display all of the files in the /etc directory that begin with the letter a and have exactly 2 characters after the t character:

Question Mark (?)

Brackets []

Brackets are used to match a single character by representing a range of characters that are possible match characters. For example, echo /etc/[te]*will print any file that begins with either a g or ucharacter and contains zero or more additional characters:

Brackets []

Exclamation Point (!)

The exclamation point is used in conjunction with the square brackets to negate a range. For example, the command echo [!TE]* will display any file that does not begin with a T or E.

Exclamation Point (!)


There are three types of quotes in BASH shell scripting, that have special significance to the Bash shell: double quotes “, single quotes ‘, and back quotes `. Each set of quotes indicates to the shell that it should treat the text within the quotes differently than it would normally be treated.

Single Quotes

Single quotes prevent the shell from doing any interpreting of special characters. This includes globs, variables, command substitution and other metacharacter that have not been discussed yet.

For example, if you want the $ character to simply mean a $, rather than it acting as an indicator to the shell to look for the value of a variable, you could execute the second command displayed below:

Single Quotes

Double Quotes

Double quotes will stop the shell from interpretingsome metacharacters, including glob characters. Within double quotes an asterisk is just an asterisk, a question mark is just a question mark, and so on. This means that when you use the second echo command below, the BASH shell doesn’t convert the glob pattern into filenames that match the pattern:

Double Quotes

Back Quotes

you probably want to execute the date command and have the output of that command sent to the echo command. To accomplish this, you would run the echo Today is `date`command line:

Back Quotes


The semicolon can be used to run multiple commands in BASH shell scripting, one after the other. Each command runs independently and consecutively.

For example, if you want to print the months of January, February and March of 2015, you can execute cal 1 2015; cal 2 2015; cal 3 2015 on the command line:


Double Ampersand (&&)

The double ampersand && acts as a logical “and” if the first command is successful, then the second command (to the right of the &&) will also run. If the first command fails, then the second command will not run.

The way that you would use the success or failure of the ls command in conjunction with && would be to execute a command line like the following;

Double Pipe

The double pipe || is a logical “or”. It works in a similar way to &&; depending on the result of the first command, the second command will either run or be skipped.

⁠⁠⁠With the double pipe, if the first command runs successfully, the second command is skipped; if the first command fails, then the second command will be run. In other words, you are essentially telling the shell, “Either run this first command or the second one”.

In the following example, the echo command will only execute if the ls command fails:

For more clarification watch this BASH shell scripting commands videos.

You may also like:





Hamza Arif
Follow us

Hamza Arif

Hey, i hamza arif student of telecommunication from BZU, i am good in Networking, Telecommunication and Web Development working on different projects and try my best to teach them to all of you.
Hamza Arif
Follow us

Leave a comment

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