If you work with Linux, you will come across the shell. It's usually the first program you work with. Graphical user interface (GUI) usage has become very popular due to its ease of use. Those who want to take advantage of the power of Linux will use the shell program by default:
- The shell is a program that provides the user with direct interaction with the operating system. Let's understand the stages in the evolution of the Linux operating system. Linux was developed as a free and open source substitute for the Unix OS. The chronology was as follows: The Unix operating system was developed by Ken Thomson and Dennis Ritchie in 1969. It was released in 1970. They rewrote Unix using C language in 1972.
- In 1991, Linus Torvalds developed the Linux kernel for the free operating system.
In this chapter, we will cover the following topics:
- Comparison of shells
- Working in shell
- Learning basic Linux commands
- Our first scriptâ
Hello World
- Compiler and interpreterâdifferences in processes
- When not to use scripts
- Various directories
- Working more effectively with shellâbasic commands
- Working with permissions
Initially, the Unix OS used a shell program called the Bourne shell. Then, eventually, many more shell programs were developed for different flavors of Unix. The following is some brief information about different shells:
sh
âBourne shellcsh
âC shellksh
âKorn shelltcsh
âenhanced C shellbash
âGNU Bourne Again shellzsh
âextension tobash
,ksh
, andtcsh
pdksh
âextension toksh
A brief comparison of various shells is presented in the following table:
Feature | Bourne | C | TC | Korn | Bash |
Aliases | no | yes | yes | yes | yes |
Command-line editing | no | no | yes | yes | yes |
Advanced pattern matching | no | no | no | yes | yes |
Filename completion | no | yes | yes | yes | yes |
Directory stacks ( | no | yes | yes | no | yes |
History | no | yes | yes | yes | yes |
Functions | yes | no | no | Yes | yes |
Key binding | no | no | yes | no | yes |
Job control | no | yes | yes | yes | yes |
Spelling correction | no | no | yes | no | yes |
Prompt formatting | no | no | yes | no | yes |
Â
What we see here is that, generally, the syntax of all these shells is 95% similar. In this book, we are going to follow Bash shell programming.
Whenever we type any text in the shell Terminal, it is the responsibility of the shell (/bin/bash
) to execute the command properly. The activities done by the shell are as follows:
- Reading text and parsing the entered command
- Evaluating meta-characters, such as wildcards, special characters, or history characters
- Process io-redirection, pipes, and background processing
- Signal handling
- Initializing programs for execution
We will discuss the preceding topics in the subsequent chapters.
Let's get started by opening the Terminal, and we will familiarize ourselves with the bash shell environment:
- Open the Linux Terminal and type in:
$ echo $SHELL/bin/bash
- The preceding output in the Terminal says that the current shell is
/bin/bash
, such as the Bash shell:
$ bash -versionGNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)Copyright (C) 2013 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html This is free software; you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.
Hereafter, we will use the word Shell
to signify the Bash shell only. If we intend to use any other shell, then it will be specifically mentioned by name, such as KORN
and other similar shells.
In Linux, filenames in lowercase and uppercase are different; for example, the files Hello
and hello
are two distinct files. This is unlike Windows, where case does
not matter.
As far as possible, avoid using spaces in filenames or directory names such as:
- Wrong filenameâ
Hello World.txt
- Correct filenameâ
Hello_World.txt
orHelloWorld.txt
This will make certain utilities or commands fail or not work as expected, for example, the make
utility.
While typing in filenames or directory names of the existing files or folders, use the tab completion feature of Linux. This will make working with Linux faster.
The following table lists a few basic Linux commands:
Command | Description |
| |
| This command is used to check the present working directory. |
| We will work in a separate directory called |
| This command will change our working directory to the newly created |
| This command can be used to verify whether we moved to the expected directory. |
| This command is used to create a new empty file called |
| This command is used to copy one file into another file. This will copy |
| This command is used to rename a file. This will rename |
| This command will display detailed information about files. |
| Let's see some magic. Rename the file using the Now, the |
| This command is used to display hidden files. |
| This command is used to delete the file. |
Since we have learned basic commands in the Linux OS, we will now write our first shell script called hello.sh
. You can use any editor of your choice, such as vi, gedit, nano, emacs, geany, and other similar editors. I prefer to use the vi editor:
- Create a new
hello.sh
file as follows:
#!/bin/bash # This is comment line echo "Hello World" ls date
- Save the newly created file.
The #!/bin/bash
line is called the shebang line. The combination of the characters #
and !
is called the magic sequence. The shell uses this to call the intended shell, such as /bin/bash
in this case. This should always be the first line in a shell script.
The next few lines in the shell script are self-explanatory:
- Any line starting with
#
 will be treated as a comment line. An exception to this would be the first line with#!/bin/bash
- The
echo
command will printHello World
on the screen - The
ls
command will display directory content in the console - The
date
command will show the current date and time
We can execute the newly created file with the following commands:
- Technique one:
$ bash hello.sh
- Technique two:
$ chmod +x hello.sh
By running any of the preceding commands, we are adding executable permissions to our newly created file. You will learn more about file permissions later in this chapter:
$ ./hello.sh
By running the preceding command, we are executing hello.sh
as the executable file. With technique one, we passed a filename as an argument to the bash shell.
The output of executing hello.sh
will be as follows:
Hello Worldhello.shSun Jan 18 22:53:06 IST 2015
Since we have successfully executed our first script, we will proceed to develop a more advanced script, hello1.sh
. Please create the new hello.sh
 script as follows:
#!/bin/bash # This is the first Bash shell # Scriptname : Hello1.sh # Written by: Ganesh Naik echo "Hello $LOGNAME, Have a nice day !" echo "You are working in directory `pwd`." echo "You are working on a machine called `uname -o`." echo "List of files in your directory is :" ls # List files in the present working directory echo "Bye for now $LOGNAME. The time is `date +%T`!"
The output of executing hello.sh
will be as follows:
Hello student, Have a nice day !.Your are working in directory /home/student/work.You are working on a machine called GNU/Linux.List of files in your directory is :hello1.sh hello.shBye for now student. The time is 22:59:03!
You will learn about the LOGNAME
, uname
, and other similar commands as we go through the book.
In any program development, the following are the two options:
- Compilation: Using a compiler-based language, such as C, C++, Java, and other similar languages
- Interpreter: Using interpreter-based languages, such as Bash shell scripting.
When we use a compiler-based language, we compile the complete source code and, as a result of compilation, we get a binary executable file. We then execute the binary to check the performance of our program.
On the other hand, when we develop the shell script, such as an interpreter-based program, every line of the program is input to the Bash shell. The lines of shell script are executed one by one sequentially. Even if the second line of a script has an error, the first line will be executed by the shell interpreter.
Shell scripts have certain advantages over compiler-based programs, such as C or C++ language. However, shell scripting has certain limitations as well.
The following are the advantages:
- Scripts are easy to write
- Scripts are quick to start and easy for debugging
- They save time in development
- Tasks of administration are automated
- No additional setup or tools are required for developing or testing shell scripts
The following are the limitations of shell scripts:
- Every line in shell script creates a new process in the operating system. When we execute the compiled program, such as a C program, it runs as a single process for the complete program.
- Since every command creates a new process, shell scripts are slow compared to compiled programs.
- Shell scripts are not suitable if heavy math operations are involved.
- There are problems with cross-platform portability.
We cannot use shell scripts in the following situations:
- Where extensive file operations are required
- Where we need data structures, such as linked lists or trees
- Where we need to generate or manipulate graphics or GUIs
- Where we need direct access to system hardware
- Where we need a port or socket I/O
- Where we need to use libraries or interface with legacy code
- Where proprietary, closed source applications are used (shell scripts put the source code right out in the open for the entire world to see)
We will explore the directory structure in Linux so that it will be useful later on:
/bin/
: This contains commands used by a regular user./boot/
: The files required for the operating system startup are stored here./cdrom/
: When a CD-ROM is mounted, the CD-ROM files are accessible here./dev/
: The device driver files are stored in this folder. These device driver files will point to hardware-related programs running in the kernel./etc/
: This folder contains configuration files and startup scripts./home/
: This folder contains a home folder of all users, except the administrator./lib/
: The library files are stored in this folder./media/
: External media, such as a USB pen drive, are mounted in this folder./opt/
: The optional packages are installed in this folder./proc/
: This contains files that give information about the kernel and every process running in the OS./root/
: This is the administrator's home folder./sbin/
: This contains commands used by the administrator or root user./usr/
: This contains secondary programs, libraries, and documentation about user-related programs./var/
: This contains variable data, such as HTTP, TFTP, logs, and others./sys/
: This dynamically creates thesys
files.
Let's learn a few commands that are required very often, such as man
, echo
, cat
, and similar:
$ man man
- From the following table, you can get an idea about various types of
man
pages for the same command:
Section number | Subject area |
1 | User commands |
2 | System calls |
3 | Library calls |
4 | Special files |
5 | File formats |
6 | Games |
7 | Miscellaneous |
8 | System admin |
9 | Kernel routines |
Â
- We can enter the
man
command to display the corresponding manual pages as follows:
$ man 1 command$ man 5 command
$ man command man -k passwd // show all pages with keyword man -K passwd // will search all manual pages content for pattern "passwd"$ man passwd
- This will show information about the
passwd
command:
$ man 5 passwd
- The preceding command will give information about the file
passwd
, which is stored in the/etc/
folder, such as/etc/passwd
.
- We can get brief information about the command as follows:
$ whatis passwd
- Output:
passwd (1ssl) - compute password hashespasswd (1) - change user passwordpasswd (5) - the password file
- Every command we type in the Terminal has an executable binary program file associated with it. We can check the location of a binary file as follows:
$ which passwd/usr/bin/passwd
- We can get complete information about the binary file location, as well as the manual page location of any command, with the following:
$ whereis passwd
- The output will be as follows:
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz /usr/share/man/man5/passwd.5.gz
- Change the user login and effective username:
$ whoami
- This command displays the username of the logged in user:
$ su
- The
su
 (switch user) command will make the user the administrator but you should know the administrator's password. Thesudo
 (superuser do) command will run the command with administrator privileges. The user should have been added to thesudoers
list.
# who am i
- This command will show the effective user who is working at that moment.
# exit
- Many a times, you might need to create new commands from existing commands. Sometimes, existing commands have complex options to remember. In such cases, we can create new commands as follows:
$ alias ll='ls -l'$ alias copy='cp -rf'
$ alias
- To remove an alias, use the following command:
$ unalias copy
- We can check operating system details, such as UNIX/Linux or the distribution that is installed with the following command:
$ uname
- Output:
Linux
This will display the basic OS information (Unix name)
- Linux kernel version information will be displayed by the following:
$ uname -r
- Output:
3.13.0-32-generic
- To get all the information about a Linux machine, use the following command:
$ uname -a
- Output:
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
- The following commands will give you more information about the Linux distribution:
$ cat /proc/version // detailed info about distribution$ cat /etc/*release# lsb_release -a.

- TheÂ
cat
 command is used for reading files and is displayed on the standard output.
- Sometimes, we need to copy a file or directory to many places. In such situations, instead of copying the original file or directory again and again, we can create soft links. In Windows, it is a similar feature to creating a shortcut.
$ ln -s file file_link
- To learn about the type of file, you can use the command file. In Linux, various types of file exist. Some examples are as follows:
- Regular file (
-
) - Directory (
d
) - Soft link (
l
) - Character device driver (
c
) - Block device driver (
b
) - Pipe file (
p
) - Socket file (
s
)
- Regular file (
- We can get information about a file using the following command:
$ file file_name
- Printing some text on screen for showing results to the user, or to ask for details is an essential activity.
- The following command will create a new file called
file_name
using thecat
command:
$ cat > file_name line 1 line 2 line 3 < Cntrl + D will save the file >
- But this is very rarely used, as many powerful editors already exist, such as vi or gedit.
- The following command will print
Hello World
on the console. Theecho
command is very useful for shell script writers:
$ echo "Hello World"
$ echo "Hello World" > hello.sh
$ echo "Hello World" >> hello.sh will append the text
- The following command will copy theÂ
Hello World
 string to thehello.sh
file: - The following command will display the content of the file on screen:
$ cat hello.sh
The following are the types of permissions:
The following are the commands for changing file permissions:
To check the file permission, enter the following command:
$ ll file_name
The file permission details are as seen in the following diagram:

In the preceding diagram, as we can see, permissions are grouped in owner-user, group, and other users' permissions. Permissions are of three typesâread, write, and execute. As per the requirement, we may need to change the permissions of the various files.
We can change the file or directory permissions in the following two ways:
We will see how Linux decides the default permissions of the newly created file or folder:
$ umask0002
The meaning of the preceding output is that, if we create a new directory, then, from the permissions of +rwx
, the permission 0002
will be subtracted. This means that for a newly created directory, the permissions will be 775
, or rwx rwx r-x
. For a newly created file, the file permissions will be rw- rw- r--
. By default, for any newly created text file, the execute bit will never be set. Therefore, the newly created text file and the directory will have different permissions, even though umask
is the same.
Another very interesting functionality is the setuid
feature. If the setuid
bit is set for a script, then the script will always run with the owner's privileges, irrespective of which user is running the script. If the administrator wants to run a script written by him by other users, then he can set this bit.
Consider either of the following situations:
$ chmod u+s file_name$ chmod 4777 file
The file permissions after any of the preceding two commands will be drwsrwxrwx
.
Similar to setuid
, the setgid
functionality gives the user the ability to run scripts with a group owner's privileges, even if it is executed by any other user:
$ chmod g+s filename
Alternatively, you can use the following command:
$ chmod 2777 filename
File permissions after any of the preceding two commands will be drwxrwsrwtx
.
The sticky bit is a very interesting functionality. Let's say, in the administration department, there are 10 users. If one folder has been set with sticky bit, then all other users can copy files to that folder. All users can read the files, but only the owner of the respective file can edit or delete the file. Other users can only read, but not edit or modify, the files if the sticky bit is set:
$ chmod +t filename
Alternatively, you can use the following command:
$ chmod 1777
File permissions after any of the preceding two commands will be drwxrwxrwt
.