Shell Script Interview Questions
Shell Script Interview Questions
Text files containing commands to execute by a shell are called shell scripts. In this,
long and repetitive series of commands are compiled into a single script that can be
stored and executed at any time, thereby reducing programming efforts. In shell
scripts, repetitive work is largely avoided. The most common reason for using shell
scripting is to program operating systems of Windows, UNIX, Apple, etc. In addition,
companies use this script to develop an operating system with their own features. In
terms of system-level operations, it is considered to be the easiest programming
language to use. A shell script is referred to as a batch file in DOS operating system,
and EXEC in IBM's mainframe VM operating systems. Shell scripts can be used for
the following applications:
Run it as follows:
$ bash [Link]
# prints
hello world
• Errors are frequent and costly, and a single error can alter the command.
• The execution speed is slow.
• Bugs or inadequacies in the language's syntax or implementation.
• Large, complex tasks aren't well suited to it.
• Contrary to other scripting languages, etc., it provides a minimal data
structure.
• Every time a shell command is executed, a new process is launched.
• Bourne Again shell (BASH): This is the most common shell available on all
Linux and based systems. It is open-source and freeware. In addition, it is an
SH-compatible shell, with improved programming and interactive features
over SH. It also allows you to efficiently perform many tasks.
• Korn shell (KSH): Korn is basically a Unix shell that was initially based on the
Bash Shell Scripting. It's a high-level language that's quite advanced. It has
associative arrays and handles the loop syntax better than Bash. It is basically
an improved version of Bourne shell.
• C shell (CSH): C shell is almost like C itself since it uses the shell syntax of the
C programming language. In most cases, a command is executed either
interactively from a terminal keyboard or from a file.
• TENEX/TOPS C shell (TCSH): TCSH does not have a specific full name. TCSH
is considered as an enhanced version of the CSH as it includes some
additional features over CSH like command-line editing and filename or
command completion. As with the previous version, it supports C-style
syntax also.
• Bourne Shell: It has compactness and speed that set it apart from other
shells. But it lacks interactive features such as the ability to recall previous
commands. Additionally, the Bourne shell does not support arithmetic and
logical expressions.
• C Shell: It is a UNIX enhancement that incorporates interactive features like
aliases and history of commands. Besides its built-in arithmetic and
expression syntax, it also includes convenient programming features.
• The C shell allows you to alias commands easily, whereas Bourne Shell does
not allow this.
• In the C shell, long commands can be used repeatedly, but not in Bourne.
• Bourne does not have access to the command history, but the C shell does.
• In the case of C, there is no need to repeatedly type the command.
Shell variables are integral parts of all Shell programs and scripts. In general, we
know that variables usually store data either in the form of characters or numbers.
Shell also stores and manipulates information using variables in its programs.
Generally, shell variables are stored as strings. Variables in the shell provide the
information needed for scripts/commands to execute. In the following example, a
shell variable is created and then printed:
variable ="Hello"
echo $variable
• Hard Link: Hard links are mirror images of the originally linked files and are
linked with an inode number. A hard-linked file remains even after the
original file is deleted. Since hard links point to inodes, they cannot be
implemented on directories. Hard links are created by the following
command:
• Soft Link: Generally, soft links (also referred to as Symbolic links) are linked
to the file name and can reside on the same as well as different file systems.
When a soft link is created or deleted, it does not affect the original file, but
when the original file is deleted, the soft link stops working. Typically, soft
links are aliases (alternative paths) for the original file. Soft links are created
by the following command:
5
Shell Script Interview Questions
We can use -h and -L operators of the test command to check whether a link is hard
or soft (symbolic link).
readlink FILE; echo $? // This returns 1 if it's a hard link and 0 if it's a symbolic
link.
The GUI (Graphical User Interface) is a client/server interface that uses graphical
icons and visual indicators to allow users to interact with devices. Specifically, it is
used to control a computer and its applications. A wide variety of applications are
supported, and it is largely dependent on the operating system.
Shell scripts or programs often contain shebang at the top. In general, the Shebang
(the series of characters "#!") is used to indicate the operating system to use a
particular interpreter to process the rest of the file. Here, the symbol '#' is referred
to as hash, and "!" is referred to as bang. This usually aids developers in avoiding
repetitive work. Scripts are generally executed by the engine; therefore, shebang
determines the location of the engine that will be used to run the script.
6
Shell Script Interview Questions
Example:
#!/bin/bash
15. What is a file system? Write down the four core components of a
Linux file system.
Generally, file systems are referred to as the collections of files, which include
information related to those files. It would be impossible to tell where one piece of
data stops and the next begins without a file system. There are four blocks in a file
system:
The tput command is an alternative command for echo. We can use this command
to control how the output is displayed. Additionally, shell scripts can do things such
as underline text and center text, regardless of the size of the screen.
Firstly, use the chmod command to set execute permission on your script as shown
below:
chmod +x [Link]
./[Link]
sh [Link]
20. Name the command that can be used to find out the number of
arguments passed to the script?
The following command will display the number of arguments passed to the
script: echo $ #
Variables inside shell scripts have a lifespan of only as long as the execution lasts.
22. What does the . (dot) indicate at the beginning of the file name? How
can it be listed?
If the file name begins with ".", it is a hidden file. When a dot appears at the
beginning of a filename, the file is hidden in most file managers and shell programs.
A Shell usually lists all the files except hidden files when you try to list the files in a
shell. Despite this, hidden files can be found in the directory. The Is command must
be run with the "–a" flag if you wish to see hidden files.
By using the command "$?", you can find out the status of the last command
executed.
For a script to run in the background, simply add "&" at the end of the command.
Example:
[Link] &
command &
8
Shell Script Interview Questions
Crontab stands for "cron table," meaning that it makes use of the cron scheduler to
perform tasks. In other words, it is the list of commands that you wish to run on a
regular schedule and the command that will let you manage it. It is possible to view
or edit the table of commands using the crontab command. In addition to the
schedule, the term "Crontab" also refers to the name of the program used to edit
the schedule.
The backup is taken using the tar command. Tar is an acronym for tape archive and
is used to create backups using tar, gzip, and bzip. Files can be saved and restored
from and to a tape using this command. In this, files and directories are generally
compressed into tarballs, an archive file. For this purpose, it is among the most
widely used commands. Furthermore, the tarball can be easily moved from one
server to another.
28. Name the command that is used to compare the strings in a shell
script.
To compare text strings, use the test command. By comparing each character in
each string, the test command usually compares text strings. In most cases, tests
are generally included as a conditional execution of shell commands. It can be used
for:
• test EXPRESSION
• test EXPRESSION && true-command
• test EXPRESSION || false-command
• test EXPRESSION && true-command || false-command
Examples:
Because 50 is greater than 40, this command prints the text "Yes".
As the two strings are identical, this command prints "0" because the expression is
true.
29. Write down the Syntax for all the loops in Shell Scripting.
In shell scripting, you can use three looping statements as given below:
• while statement
• for statement
• For Loop: Loops that operate on lists of items are known as loops. Each item
in the list receives the same set of commands.
Syntax:
Example:
for a in 1 2 3 4 5 6 7 8 9 10
do
if [ $a == 3 ]
then
break
fi
echo "Iteration no $a"
done
Output”
$bash -f [Link]
Iteration no 1
Iteration no 2
• While Loop: It involves evaluating a command first and then executing a loop
based on the result. The loop will be terminated if the command raises to
false.
Syntax:
while command
do
10
Shell Script Interview Questions
Statement to be executed
done
Example:
a=0
while [ $a -lt 3 ]
do
echo $a
a=`expr $a + 1`
done
Output:
$bash -f [Link]
0
1
2
3
3
31. How will you pass and access arguments to a script in Linux?
$1 , $2 .. $n
With set, you can turn on or off debugging options in the shell:
• Set -x: This displays commands and their arguments as they are being
executed.
• Set -v: It displays shell input lines in real-time as they are read.
11
Shell Script Interview Questions
33. What is the best way to debug the shell script/program problems?
• The shell script can be modified to include debug statements to display the
information that can be useful in identifying the problem.
• By setting -x in the script, we can enable debugging.
• sed Command: sed command is an acronym for stream editor. It is used for
editing a file without using an editor. The SED command performs a variety
of operations on files, such as searching, finding and replacing, inserting and
deleting. However, substitution or find and replace are the most commonly
used features of SED.
Syntax: sed options file
Example: Execution over Shell Interpreter/Editor
36. Name the command that is used to display the shell's environment
variable.
Shell functions, along with other Linux programs, are controlled by environmental
variables. Any child process of the shell has access to an environment variable.
These variables are necessary for some programs to function
properly. env or printenv commands can be used to display the shell's environment
variables.
12
Shell Script Interview Questions
37. Name different commands that are available to check the disk usage.
39. How will you find total shells available in your system?
Within your system, there are several shells available. If you want to find all the
shells on the system, you can use $ cat /etc/shells.
Example:
$ cat /etc/shells
$ cat /etc/shells
Output:
/bin/sh
/bin/bash
/sbin/nologin
13
Shell Script Interview Questions
/bin/ksh
/bin/csh
41. Name the command that one should use to know how long the
system has been running.
Using the Uptime command, you can see how long your system has been active.
You can also determine the number of users with running sessions, and the average
system load for 1, 5, and 15 minutes. The information displayed at once can also be
filtered based on your specified options.
Example: $ uptime
Entering the above command at the shell prompt, namely $ uptime, will produce the
following output:
You can use $$ to get the process id of the current process. However, $! displays
the process id for a background process that recently went away.
Example:
grep “apple” [Link] //Displays all the lines with the word “apple” in the file1
grep “apple” [Link] [Link] //Scan multiple documents and search the word “apple” in both files.
• Find command: The FIND command searches for files and folders based on
their size, modification time, and access time. You can use this command to
search files and directories. Find command have the following syntax:
find <path> <search criteria> <action>
Example:
14
Shell Script Interview Questions
find . –name [Link] //Command will find [Link] in the current directory.
function_name ()
{
list of commands
}
Function_name is the name of your function, and that's what you'll use to call it
from anywhere in your script. The function name must be followed by parentheses,
then a list of commands enclosed in braces.
Example:
#!/bin/sh
# Define your function here
Hello ()
{
echo "Hello World"
}
$./[Link]
Hello World
Pipe command allows you to use several commands in the same way, in which the
output of one is used as input for another. Like a pipeline, each process output is
directly input to the next one. A pipe is represented by the symbol "|". The flow of
data through a pipeline is unidirectional, i.e., from left to right.
Syntax :