Linux Shell Scripting Tutorial: The Basics

Sarath Pillai's picture
Linux Shell Scripting

If you are a Linux or a Unix user, then you are already using a shell by default. Every user in Linux uses a shell to interact with the system. Shell is nothing but a program that let’s you do your day to day activities. Linus Torvalds said the below in one of his interviews.

 

 

“Nobody Really Uses An Operating System. People Use Programs on their computer, The only mission in life of an operating system is to help those programs run. Operating system never does anything on its own, Its only waiting for programs to ask for certain resources, ask for a certain file on the disk, or connect to outside world.” --- Linus Torvalds(Creator of Linux Kernel)

 

So basically you never ever interact directly with an operating system, you always use programs to interact with the operating system. Everything that you are doing on a computer/or server are all via programs. No matter what.

 

shell is nothing but a program that lets you interact with the operating system. Your interaction with the operating system will always be using some sort of program. Shell let's you start other programs and do different things in an operating system. Shell is a user interface for you to execute programs. User interface(shell) is independent of the operating system.

 

The word shell itself originates from the fact that its a layer(outer shell of the operating system) on top of the operating system, which lets you execute other programs and commands.

 

Similar to operating systems, there are different flavors and distributions of shells. Different shell programs have its own set of features and commands as well. Selection of a shell depends on your use case and requirement. This tutorial series will be primarily focusing upon bash shell.

 

Shell being a program that is completely independent of the operating system itself led to the development of different shells. Bash shell is the most popular among the different shells that were developed, and enjoys widespread adoption in almost all nix family of operating systems. In fact bash is the default shell in most Linux distributions out there.

 

 

Bourne shell was the first widely used shell. The name Bourne comes from the creator(Steven Bourne). It was created at Bell Labs(the same place where Unix was born). It is a popular choice in most Unix operating systems(not Linux). In fact its still used in many major Unix versions out there. Bourne shell was released during 1977. It included programming functionalities like creating variables, flow controls etc. Unix version 7 included Bourne shell. The shell is available in Unix like operating systems as /bin/sh.

 

As mentioned earlier, we will be concentrating more on bash shell. Bash stands for Bourne Again Shell. It was created by Biran Fox during 1988. Bash was written as part of the GNU Project.  You might be thinking what is GNU Project ?.

 

GNU Project was something that was started by Richard Stallman. The main motive of the GNU project was to make an operating system very much similar to UNIX(but not Unix.). A modern operating system has so many programs in it. As we discussed earlier an operating system without any program for the user to interact is useless(as we humans always interact with programs). Richard Stallman and other's in the GNU project started writing replacement programs for all Unix programs. If you are working in a GNU/Linux distribution (like Ubuntu, Red Hat, Suse, Centos etc.), you are actually making use of GNU programs.

 

 

Bash itself is a GNU program written as replacement for Bourne shell in Unix. You can find the list of all GNU programs that we use in day to day life in the below link.


List of GNU Programs
 

Bash is the default shell for all users out there in all Linux distributions.

 

When you are logged into a server the environment you get after logging in is generally bash shell. This is because bash shell is the program that provides you an interface to interact with the operating system and trigger other programs and commands. 

 

Let's see how it looks like after you log into a sever.  The interface that you get after you login to a server is called a terminal. A shell terminal. Below shown are the shell terminal appearance you get in the case of a RedHat & Ubuntu based distributions.

 

In RedHat the terminal by default looks like the below:

 

[sarath@www ~]$

 

In Ubuntu/Debian based OS, the terminal by default looks like the below.

 

ubuntu@www:~$

 

There is a reason why the terminal looks that way. This is because of a particular configuration setting in bash shell. This can be altered at your will as you alter settings for any program.

 

Shell related settings are mostly configured using variables. We will be covering variables in detail later, but for now just consider a variable as a placeholder to hold user given values. Bash will alter its behavior and appearance depending upon the values assigned by the user to these variables.

 

The appearance of the shell is controlled by a variable called PS1(ie: the shell prompt as we saw in the above example.). As this is a variable, we can change its value(the placeholder's value.). The current value of the variable can be seen as shown below.

 

[root@www ~]# echo $PS1
[\u@\h \W]\$

 

The above shown output is from a RedHat based distribution. The output should be similar or slightly different in any other distribution (the default appearance does not matter at all because you can change the PS1 placeholder's value the way you like.).

echo command is a shell builtin command that is primarily used to display a given string to the screen(ie: print something to the screen.). shell builtin means, its part of the bash program itself(ie: echo command is not an external program..Its part of bash program itself. Bash provides a lot of builtin commands similar to echo.)

You can identify if a command is part of bash or is an external program, using another command called type.

 

root@www:~# type echo
echo is a shell builtin

 

If a particular command is not part of the bash shell, type will show that as well(along with the path where the command is actually located.)

 

root@www:~# type mkdir
mkdir is /bin/mkdir

 

Now let's understand the PS1 variable(placeholder) a bit more. The output of PS1 as shown previously typically will look like [\u@\h \W]\$. Let's understand what that means.

 

  • u stands for the current username(the username using which you are currently logged into)
  • h hostname of the machine. Well only till first "."..So if hostname of the server is www.example.com, this field will show www(ie: till the first .)
  • W Current working directory (it will show ~ symbol, if the user is in the home directory.)

 

If you open the man pages of bash, you can see all values available for you use customize PS1 variable(under the section PROMPTING).

Well man is nothing but a method to access documentations in GNU/Linux Operating systems. You can open manual pages for a program using the man command as shown below. The below shown command will open docs for bash itself, you can find the things mentioned above under PROMPTING section.

 

#man bash

 

How to change these placeholder values or variables in Bash shell to alter the behaviour or appearance of the shell?

 

Changing these placeholder values is really simple. Its as simple as saying PS1 is equal to the new value of your interest(shown below.). We will use equal to operator to assign a value.

 

[root@www ~]# PS1='[\u@\H \W]\$'
[root@www.example.com ~]#

 

In the above shown example, i changed lowercase h in PS1 to uppercase H. And immediately it got reflected. Uppercase H stands for the full hostname.

 

Similar to PS1 placeholder(variable), there is PS2 as well. Let's see what's the default value of PS2 variable.

[root@www.example.com ~]#echo $PS2
>

 

 

PS2 variable value is >. Please remember the fact that these placeholder's are nothing but settings for bash shell. The behaviour or appearance will change depending upon the values assigned to these placeholders.

 

If you type some incomplete command and press enter, bash shell will show you PS2 value. This is to indicate that the previous command is incomplete, and you need to complete it. For example, let me purposely type in an incomplete ls command.

 

[root@www.example.com ~]#ls -l '
>

 

 

See the > above. Similar to PS1, you can change PS2 value as well. Let's change it and confirm by mistyping/incomplete ls command again.

 

[root@www.example.com ~]#PS2=+
[root@www.example.com ~]#ls -l '
+

In the above example, i have assigned + value to PS2 variable, and the incomplete command output reflects the same.

 

Similar to PS1 & PS2, there is another placeholder(variable) called PATH. This PATH variable is of utmost importance, because this variable tells bash shell about the locations to search for a command or program.

 

Bash has a lot of user friendly features that helps a user to find a command(command is a program) without completely remembering it. In fact if you ask shell to show all commands that begins with the letter "a", it will show you that. You can go on typing letters and ask shell for suggestions. This is triggered using TAB charecter on the keyboard.

If i type "ls" in the terminal and press TAB charecter multiple times on the keyboard, shell will show you suggestions of command's that begins with "ls"(shown below). This way you can easily find the command that you are looking for.

 

[root@www.example.com ~]#ls
ls        lsblk     lscpu     lslocks   lsmod     lspci     lss3-2.7  
lsattr    lscgroup  lsinitrd  lslogins  lsof      lss3      lssubsys 

 

You might be thinking "How does shell remember all the commands that begins with the letters that we type in ?". Well actually shell does not remember all the commands that begins with a specific letter or word. As soon as you type a letter or word and then press TAB, the shell starts searching for programs that begins with that letter or word in a set locations. The search happens in multiple locations that the user specify using PATH variable.

 

[root@www.example.com ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin

 

In the above shown output of PATH variable, you can see the list of locations that the shell does the search operation to find programs and commands. Each location is seperated by : symbol.

 

Similar to any other variable, PATH variable can also be changed/modified by adding a new location for the shell to search.

 

PATH=${PATH}:/opt/bin

 

 

In the above variable assignment, PATH variable is modified with a new additional path to search (/opt/bin). ${PATH} will expand to already existing list of locations (shown earlier), and add one more to it from our side /opt/bin.

 

PATH variable is really important for the shell to function properly and give you suggestions or provide you the user friendly nature of letting you type the command directly. Without PATH variable, you will have to execute a program using its full location. For example, you can never execute ls command as it is without PATH variable. ls command will have to be executed using /bin/ls. This is because without the PATH variable, shell does not know where ls program is located. Hence you will have to provide correct location and then the command.

 

Similar to bash shell variables, there are other variables as well that are completely tool specific. System tools like ls command, less command have their own variables. If you have set values to these tool specific variables, then they will behave/format the outputs accordingly. For example, LS_OPTIONS variable is for ls command. LESS variable is for less command.

 

LS_OPTIONS='--color=yes' will add colours to the ls command output. LESS='-X' will tidy the appearance for less command output.

Apart from all these bash specific and tool specific variables, you can also have your own script specific variables which you want to use. We will cover that part in a dedicated section called variables in bash.

 

What is a Shell Script?

We already understood the fact that shell is the primary user interface using which a user can interact with the operating system. Using shell users can execute programs. Different commands(programs) achieve different results in the system. A script is nothing but a file containing a series of commands(programs) in the correct order to achieve a particular result.

An example can be a backup script (a script that takes regular backup) that takes backup of a directory, and sends an email alert when done. Another example can be a monitoring script that monitor's memory usage on the server, and send alert's depending upon the percentage of memory being consumed.

 

How Do I Write A Shell Script?

As mentioned above, a script is nothing but a file containing a series of commands. So you can create it very easily. Let's create an example script to understand it practically.

vim example_script.sh

 

The content of the example_script.sh file should look something like the below. This example script of ours only prints a statement on the screen using echo command. So our first script only contains one single command.

#!/bin/bash
echo "This is our first script"

 

Ok. So Now I Have My First Script Ready. But How to Execute this shell script?

Executing a shell scritpt can be done in multiple ways. The first step before you can execute it is to provide the executable permission. Executable permission can be given using the chmod command as shown below.

chmod +x example_script.sh

+x option shown above will give executable permission to the user, group and everybody else in the system. You can verify the permission status of the file by executing ls -l command as shown below.

root@localhost:~# ls -l example_script.sh
-rwxr-xr-x 1 root root 44 Aug 16 08:08 example_script.sh

 

Now we can execute the script using the below method.

 

Method to Execute a shell script:

This method is quite simple. Just prepend ./, to the script file name as shown below.

root@localhost:~# ./example_script.sh
This is our first script

 

 

We can clearly see from the above output that our shell script worked the way we required it to work. It does the job of printing a statement to the screen. The above shown method is not the only method available to execute a shell script. There are other methods as well. Proceed to the next article in this tutorial series to understand different methods of script execution and their differences.

 

Continue To Next Article in This Tutorial Series: Different methods of Executing a shell script in Linux

Rate this article: 
Average: 3.4 (69 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.