Shell Scripting Interview Questions

Ratings:
(4.3)
Views:1758
Banner-Img
  • Share this blog:

Shell Scripting Interview Questions

Following are some of the most frequently asked Shell Scripting interview questions in the interview, here are the answers to them.

Q1) Write down syntax for all loops in shell scripting?

Ans:

For loop:

for i in $( ls ); do
echo item: $i
done

While loop:
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done

Until loop:

#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done

Q2) What it means by #!/bin/sh or #!/bin/bash at the beginning of every script?

Ans: That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there will be #!/usr/bin/python.

Q3) What command "export" do?

Ans: Makes variable public in subshells.

Q4) How to run the script in the background?

Ans: add "&" to the end of the script.

Q5) What "chmod 500 script" do?

Ans: Makes script executable for script owner.

Want to acquire industry skills and gain complete knowledge of Shell Scripting? Enrol in Instructor-Led live UNIX Shell Scripting Training to get Job Ready!

Q6) What ">" do?

Ans: Redirects output stream to file or another stream.

Q7) What difference between & and &&

Ans:

& - we use it when we want to put the script to the background

&& - when we want to execute a command/script if the first script was finished successfully

Q8) When do we need "if" before [ condition]?

Ans: When we need to run several commands if the condition meets.

Q9) What would be the output of the command: name=John && echo 'My name is $name'

Ans: My name is $name

Q10) Which is the symbol used for comments in bash shell scripting?

Ans: #

Q11) What would be the output of the command: echo ${new:-variable}

Ans: variable

Q12) What difference between ' and "quotes?

Ans:

  • ' - we use it when do not want to evaluate variables to the values.
  • " - all variables will be evaluated and their values will be assigned instead.

Q13) How to print all arguments provided in the script?

Ans: echo $* or echo $@

Q14) What difference between [ $a == $b ] and [ $a -eq $b ]

Ans: [ $a == $b ] - should be used for string comparison [ $a -eq $b ] - should be used for number tests

Q15) What difference between = and ==

Ans:

  • = - we using to assign value to variable.
  • == - we using for string comparison.

Q16) Write the command to test if $a greater than 12?

Ans: [ $a -gt 12 ]

Q17) Write the command to test if $b is less or equal to 12?

Ans: [ $b -le 12 ]

Q18) How to redirect stdout and stderr streams to the log.txt file from the script inside?

Ans: Add "exec >log.txt 2>&1" as the first command in the script.

Q19) How to get part of the string variable with the echo command only?

Ans:
echo ${variable:x:y} x - start position y - length example: variable="My name is Petras, and I am developer." echo ${variable:11:6} # will display Petras

Q20) How to get home_dir with echo command only if string variable="User:123:321:/home/dir" is given?

Ans:
echo ${variable#*:*:*:} or echo ${variable##*:}

Q21) How to get “User” from the string above?

Ans:
echo ${variable%:*:*:*} or echo ${variable%%:*}

Q22) How to list users which UID less than 100 (awk)?

Ans: awk -F: '$3<100' /etc/passwd.

Q23) Compare Numeric Operators

Ans:

Operator Syntax Description
eq INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
ge INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
gt INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
le INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
lt INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
ne INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2

Q24) Syntax to define an array in bash?

Ans: array=("Hi" "my" "name" "is")

Q25) Print the first array element.

Ans: echo ${array[0]}

Q26) Print all array elements.

Ans: echo ${array[@]}

Q27) Print all array indexes syntax?

Ans: echo ${!array[@]}

Q28) Syntax to remove array element with id 123?

Ans: unset array[123]

Q29) Syntax to add a new element to the array with id 1980?

Ans: array[1980]="New_element"

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox