25 Linux Shell Scripting Interview Questions and Answers

Q:1 What is Shell Script and why it is required ?

Ans: A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish day to day tasks, we can add these all commands together in a text file (Shell Script) to complete daily routine task. In other words we can shell is required for the automation.

Q:2 What is the default login shell and how to change default login shell for a specific user ?

Ans: In Linux like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change default shell using the “chsh” command . Example is shown below :

# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh

Q:3 What are the different type of variables used in a shell Script ?

Ans: In Linux shell script we can use two types of variables :

  • System defined variables
  • User defined variables

System defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command. To display the value of system defined variables use echo command, examples are  echo $PWD or echo $HOME
User defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo $<Name_of_Variable>

Q:4 How to redirect both standard output and standard error to the same location ?

Ans: There two methods to redirect std output and std error to the same location. These methods are listed below:

Method 1)  2>&1 (# ls /usr/share/doc > out.txt 2>&1 )

Method 2)  &> (# ls /usr/share/doc &> out.txt )

Q:5 What is the Syntax of “nested if statement” in shell scripting ?

Ans : Basic Syntax is shown below :

if [ Condition ]
if [ condition ]

Q:6 What is the use of “$?” sign in shell script ?

Ans: While writing a shell script , if you want to check whether previous command is executed successfully or not , then we can use “$?”. Use echo command to print the vlaue of ‘$?’ variable. This variable is generally used in if statement to check the exit status of previous command. Basic example is shown below :

root@localhost:~# ls /usr/bin/shar
root@localhost:~# echo $?

If exit status is 0 , then command is executed successfully

root@localhost:~# ls /usr/bin/share

ls: cannot access /usr/bin/share: No such file or directory
root@localhost:~# echo $?

If the exit status is other than 0, then we can say command is not executed successfully.

Q:7 How to compare numbers in Linux shell Scripting ?

Ans: We can compare the numbers in shell script by using parameters like ‘-gt’ (greater than), ‘-eq’ (equals to) and  ‘-lt’ (less than) in if statement. Example is shown below :


if [ $x -gt $y ]
echo “x is greater than y”
echo “y is greater than x”

Q:8 What is the use of break command ?

Ans: The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops.

Q:9 What is the use of continue command in shell scripting ?

Ans The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where error has occurred but we still want to execute the next commands of the loop.

Q:10 Tell me the Syntax of “Case statement” in Linux shell scripting ?

Ans: The basic syntax is shown below :

case word in

Q:11 What is the basic syntax of while loop in shell scripting ?

Ans: Like the for loop, the while loop repeats its block of commands a number of times. Unlike the for loop, however, the while loop iterates until its while condition is no longer true. The basic syntax is :

while [ test_condition ]

Q:12 How to make a shell script executable ?

Ans: Using the chmod command we can make a shell script executable. Example is shown below :

# chmod a+x myscript.sh

Q:13 What is the use of “#!/bin/bash” ?

Ans: #!/bin/bash is the first of a shell script , known as shebang , where # symbol is called hash and ‘!’ is called as bang. It shows that command to be executed via /bin/bash.

Q:14 What is the syntax of for loop in shell script ?

Ans: Basic Syntax of for loop is given below :

for variables in list_of_items

Q:15 How to debug a shell script ?

Ans: A shell script can be debug if we execute the script with ‘-x’ option ( sh -x myscript.sh). Another way to debug a shell script is by using ‘-nv’ option ( sh -nv myscript.sh).

Q:16 How compare the strings in shell script ?

Ans: test command is used to compare the text strings. The test command compares text strings by comparing each character in each string.

Q:17 What are the Special Variables set by Bourne shell for command line arguments ?

Ans: The following table lists the special variables set by the Bourne shell for command line arguments.


Q:18 How to test files in a bash shell script ?

Ans: test command is used to perform different test on the files. Basic test are listed below :


Q:19 How to put comments in your bash shell script ?

Ans: Comments are the messages to yourself and for other users that describe what a script is supposed to do and how it works. To put comments in your script, start each comment line with a hash sign (#) . Example is shown below :

# This is a command
echo “I am logged in as $USER”

Q:20 How to get input from the terminal for shell script ?

Ans: ‘read’ command reads in data from the terminal (using keyboard). The read command takes in whatever the user types and places the text into the variable you name. Example is shown below :

# vi /tmp/test.sh

echo ‘Please enter your name’
read name
echo “My Name is $name”

# ./test.sh
Please enter your name
My Name is LinuxTechi

Q:21 How to unset or de-assign variables in Linux ?

Ans: ‘unset’ command is used to de-assign or unset a variable. Syntax is shown below :

# unset <Name_of_Variable>

Q:22 How to perform arithmetic operation in Linux ?

Ans: There are two ways to perform arithmetic operations :

1. Using expr command (# expr 5 + 2 )
2. using a dollar sign and square brackets ( $[ operation ] )

Example : test=$[16 + 4] ; test=$[16 + 4]

Q:23 Basic Syntax of do-while statement ?

Ans: The do-while statement is similar to the while statement but performs the statements before checking the condition statement. The following is the format for the do-while statement:

} while (condition)

Q:24 How to define functions in shell scripting ?

Ans: A function is simply a block of of code with a name. When we give a name to a block of code, we can then call that name in our script, and that block will be executed. Example is shown below :

$ diskusage () { df -h ; }

Q:25 How to use bc (bash calculator) in a bash shell script ?

Ans: Use the below Syntax to use bc in shell script.

variable=`echo “options; expression” | bc`

Read Also: 20 Linux Commands Interview Questions and Answers

Share on:

I am a Cloud Consultant with over 15 years of experience in Linux, Kubernetes, cloud technologies (AWS, Azure, OpenStack), automation (Ansible, Terraform), and DevOps. I hold certifications like RHCA, CKA, CKAD, CKS, AWS, and Azure.

4 thoughts on “25 Linux Shell Scripting Interview Questions and Answers”

  1. Great Collection. Also it would be great if you can share common shell script errors and error handling related stuff.


Leave a Comment