Shell Scripting Interview Questions

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.

Q: Write down syntax for all loops in shell scripting?

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

Q: What it means by #!/bin/sh or #!/bin/bash at beginning of every script?

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.

Q: What command "export" do?

Makes variable public in subshells.

Q: How to run the script in the background ?

add "&" to the end of the script.

Q: What "chmod 500 script" do?

Makes script executable for script owner.

Q: What ">" do?

Redirects output stream to file or another stream.

Q: What difference between & and &&

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

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

Q: When we need "if" before [ condition]?

When we need to run several commands if condition meets.

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

My name is $name

Shell Scripting Interview Questions

Q: Which is the symbol used for comments in bash shell scripting?

#

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

variable

Q: What difference between ' and "quotes?

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

Q: How to print all arguments provided to the script ?

echo $*

or

echo $@

Q: What difference between [ $a == $b ] and [ $a -eq $b ]

[ $a == $b ] - should be used for string comparison

[ $a -eq $b ] - should be used for number tests

 Q: What difference between = and ==

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

Q: Write the command to test if $a greater than 12?

[ $a -gt 12 ]

Q: Write the command to test if $b less or equal 12?

[ $b -le 12 ]

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

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

Q: How to get part of string variable with echo command only?

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

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

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

or

echo ${variable##*:}

Q: How to get “User” from the string above?

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

or

echo ${variable%%:*}

Shell Scripting Interview Questions

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

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

Q: Compare Numeric Operators

OperatorSyntaxDescription
eqINTEGER1 -eq INTEGER2INTEGER1 is equal to INTEGER2
geINTEGER1 -ge INTEGER2INTEGER1 is greater than or equal to INTEGER2
gtINTEGER1 -gt INTEGER2INTEGER1 is greater than INTEGER2
leINTEGER1 -le INTEGER2INTEGER1 is less than or equal to INTEGER2
ltINTEGER1 -lt INTEGER2INTEGER1 is less than INTEGER2
neINTEGER1 -ne INTEGER2INTEGER1 is not equal to INTEGER2

 

Q: Syntax to define array in bash?

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

Q: Print the first array element ?

echo ${array[0]}

Q: Print all array elements ?

echo ${array[@]}

Q: Print all array indexes syntax?

echo ${!array[@]}

Q: Syntax to remove array element with id 123 ?

unset array[123]

Q: Syntax to add new element to the array with id 1980 ?

array[1980]="New_element"