Linux OS Shell Interview Questions & Answers
Download PDF

Linux OS Shell frequently Asked Questions by expert members with experience in Linux Shell. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts

53 Linux Shell Questions and Answers:

Linux Shell Interview Questions Table of Contents:

Linux Shell Job Interview Questions and Answers
Linux Shell Job Interview Questions and Answers

2 :: What is the output of this program?

#!/bin/bash
san_var=10
echo "the value of "san_var" is $san_var"
exit 0
a) the value of "san_var" is 10
b) the value of is 10
c) the value of san_var is $san_var
d) the value of "san_var" is $san_var

a) the value of "san_var" is 10
Output:
root@ubuntu:/home/google# ./test.sh
the value of "san_var" is 10
root@ubuntu:/home/google#

3 :: What is the output of this program?

#!/bin/bash
san_var=hello
readonly san_var
san_var=hi
echo $san_var
exit 0
a) hello
b) hi
c) nothing will print
d) none of the mentioned

a) hello
Explanation:
After the execution of the 'readonly' command, shell will not provide the permission to overwrite the value stored in variable 'san_var'.
Output:
root@ubuntu:/home/google# ./test.sh
./test.sh: line 4: san_var: readonly variable
hello
root@ubuntu:/home/google#

4 :: What is the output of this program?

#!/bin/bash
var[1]=san_1
var[2]=san_2
var[3]=san_3
echo ${var[*]}
exit 0
a) san_1
b) san_2
c) san_3
d) san_1 san_2 san_3

d) san_1 san_2 san_3
Explanation:
All items of an array can be accessed by using ${[*]} or ${[@]}.
Output:
root@ubuntu:/home/google# ./test.sh
san_1 san_2 san_3
root@ubuntu:/home/google#

5 :: What is the output of this program?

#!/bin/bash
var1=10
$var1=20
echo $var1
exit 0
a) program will print 10
b) program will generate a warning message
c) program will print 20
d) both (a) and (b)

d) both (a) and (b)
Explanation:
The doller sign ($) is used to access a variable's value, not to define it.
Output:
root@ubuntu:/home/google# ./test.sh
./test.sh: line 3: 10=20: command not found
10
root@ubuntu:/home/google#

6 :: What is the output of this program?

#!/bin/bash
san_var="google"
echo "$san_var"
echo '$san_var'
echo '"$san_var"'
echo "'$san_var'"
echo $san_var
exit 0
a) google
$san_var
"$san_var"
'google'
$san_var
b) google
google
"google"
'google'
google
c) program will generate an error message
d) program will print nothing

a) google
$san_var
"$san_var"
'google'
$san_var

Explanation:
Using double quotes does not affect the substitution of the variable, while single quotes and backslash do.
Output:
root@ubuntu:/home/google# ./test.sh
google
$san_var
"$san_var"
'google'
$san_var
root@ubuntu:/home/google#

8 :: Which one of the following is not a valid shell variable?
a) _san
b) san_2
c) _san_2
d) 2_san

d) 2_san
Explanation:
The shell variable can contain only letters(a to z or A to Z), numbers(0 to 9), or a underscore character(_) and a variable can not start with a number.

12 :: What is the output of this program?

#!/bin/sh
echo "Just call the function"
san_function
san_function() {
echo "This is a function"
}
exit 0
a) only first string will print without any error
b) only second string will print without any error
c) both strings will print
d) none of the mentioned

d) none of the mentioned
Explanation:
Function must be defined prior to call. Hence only first string will print and program will generate an error also.
Output:
root@ubuntu:/home/globalguideline# ./test.sh
Just call the function
./test.sh: 3: san_function: not found
root@ubuntu:/home/globalguideline#

14 :: What is the output of this program?

#!/bin/sh
var="google"
san_function() {
var="Linux"
echo $var
}
san_function
exit 0
a) google
b) Linux
c) command not found
d) none of the mentioned

b) Linux
Explanation:
If local variable name is same as the global variable, it overlays the variable, but only within the function.
Output:
root@ubuntu:/home/google# ./test.sh
Linux
root@ubuntu:/home/google#

15 :: What is the output of this program?

#!/bin/sh
san_function() {
echo "Welcome to the google"
printf "World of Linuxn"
}
unset -f san_function
san_function
exit 0
a) Welcome to the google
b) World of Linux
c) both (a) and (b)
d) nothing will print

d) nothing will print
Explanation:
Function definition was deleted before calling the function. command 'unset -f function_name' deletes the function definition.
Output:
root@ubuntu:/home/google# ./test.sh
./test.sh: 6: san_function: not found
root@ubuntu:/home/google#

20 :: Which of the following command provides the list of the functions defined in the login session?
a) declare -f
b) declare -F
c) both (a) and (b)
d) none of the mentioned

c) both (a) and (b)
Explanation:
'declare -F' provides just the name of the functions and 'declare -f' provides their definitions also.