Top 10 Shell Scripting Interview Questions with Answers

Here you will learn top 10 linux shell scripting interview questions with answers. Please practice these questions as 90% questions will be asked during in linux admin interview.

Q:1 How many types of variables used in a shell Script?

Ans: There are two types of variables used in Shell Scripts:

  • System Variables
  • User defined Variables
  • System variables are defined or created by OS itself. These variables are generally defined in Capital Letters and we can check these variables by ‘printenv’ command.
SHELL=/bin/bash
TERM=xterm
USER=testuser
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca:…
MAIL=/var/mail/testuser
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
PWD=/home/testuser
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/testuser
LOGNAME=testuser
LESSOPEN=| /usr/bin/lesspipe %s
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/printenv
  • User defined variables are created by users and the values of variables can be viewed by using the command “echo $”.
var1=20
var2=testing
var3=“testing not done yet”

Q:2 How to check previous command execution status in shell script ?

Ans: While writing a shell script , if you want to check previous command is executed successfully or not , then we use “ echo $?” with if statement to check the exit status of previously executed command.

[email protected]:~# ls /usr/bin/pwd
/usr/bin/pwd
[email protected]:~# echo $?
0
If exit status is 0 , then command was executed successfully
[email protected]:~# ls /usr/bin/linux
ls: cannot access /usr/bin/linux: No such file or directory
[email protected]:~# echo $?
2
If the exit status is other than 0, then we can say command is not executed successfully.

Q:3 What is the basic syntax of WHILE loop in shell scripting ?

Ans: The while loop repeats its block of commands a number of times. The while loop iterates until its while condition is no longer true. The basic syntax is .

while [ test_condition ]
do
command
done

Q:4 What is the syntax of FOR loop in shell script ?

Ans: Basic Syntax of for loop is given below :

for variable in list_of_items
do
command1
command2
….
last_command
done

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

Ans: Comments are the messages for other users that describe the functionality of a shell script. To put comments in your script, start each comment line with a hash sign (#) as below.

#!/bin/bash
#This is a comment
#The below command show the logged in user
echo “I am $USER”

Q:6 How to get input from the command terminal in a shell script?

Ans: ‘read’ command reads in data from the command line terminal . The read command takes input whatever the user types and places the text into the variable you name as shown below.

# vim /tmp/test.sh
#!/bin/bash
echo ‘Please enter your name’
read name
echo “My Name is $name”

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

Q:7 What is the syntax of IF ELSE Conditionals in shell script?

Ans: The if..else statement is the control statement that allows shell to make correct decision out of two conditions. The syntax as shown below.

if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi

Q:8. What is Shebang in shell script?

Ans: Shebang is a # sign followed by an exclamation i.e. !. Generally, this can be seen at the top of the script. Usually, a developer or administrator uses this to avoid repetitive work. Shebang determines the location of the engine which is to be used in order to execute the script.

Here ‘#’ symbol is called hash and ‘!’ is called a bang.

Instance: #!/bin/bash

The above line tells which shell to use.

Q:9 What is the significance of $# ?

Ans: $# shows the count of the arguments passed to a script.

Q:10 What is the difference between $* and [email protected] ?

Ans: [email protected] considers each quoted arguments as separate arguments whereas $* will consider the entire set of positional parameters as single string.

Hence, you have gone through top 10 linux shell scripting interview questions . I hope you are feeling confident.

I hope I helped.. Thanks!!!

Also Read : Difference between Soft Links and Hard Links in Linux

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

3 thoughts on “Top 10 Shell Scripting Interview Questions with Answers”

Leave a Comment