Top Most Linux Shell Interview Questions
Q – 1 To redefine a variable, it can be removed from the list of variables by using the command:
a) unset
b) delete
c) remove
d) clear
Ans- a) unset
Q – 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
Ans- a) the value of “san_var” is 10
Q – 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
Ans- a) hello
Q – 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
Ans- d) san_1 san_2 san_3
Q – 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)
Ans- d) both (a) and (b)
Q – 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
Ans- a) google
Q – 7 In the shell, by default, all variables are considered and stored as:
a) string
b) integer
c) character
f) float
Ans- a) string
Q – 8 Which one of the following is not a valid shell variable?
a) _san
b) san_2
c) _san_2
d) 2_san
Ans- d) 2_san
Q – 9 Which one of the following statement is true about variables in shell?
a) variables do not require declaration before assigning value to them
b) variables are case sensitive
c) to extract the contents of a variable, we have to provide the variable a preceding $
d) all of the mentioned
Ans- d) all of the mentioned
Q – 10 Which command reads user input from the terminal and assign this value to a variable name?
a) read
b) get
c) declare
d) set
Ans- a) read
Q – 11 What is the output of this program?
#!/bin/sh
san_function1() {
a=5
echo “This is the first function”
san_function2
}
san_function2() {
echo “This is the second function”
san_function3
}
san_function3() {
echo “This is the third function”
}
san_function1
exit 0
a) This is the first function
This is the second function
This is the third function
b) This is the first function
This is the third function
This is the second function
c) This is the second function
This is the first function
This is the third function
d) This is the third function
This is the first function
This is the second function
Ans- a) This is the first function
This is the second function
This is the third function
Q – 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
Ans- d) none of the mentioned
Q – 13 What is the output of this program?
#!/bin/bash
function san_function1 {
echo “This is first function”
}
san_function2() {
echo “This is second function”
}
san_function1
san_function2
exit 0
a) This is the first function
b) This is the second function
c) This is the first function
This is the second function
d) program will generate error because first function definition is not correct
Ans- c) This is the first function
This is the second function
Q – 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
Ans- b) Linux
Q – 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
Ans- d) nothing will print
Q – 16 Parameters can be passed to a function:
a) by using the parameter variables $1, $2, $3.
b) by using the environment variables
c) both (a) and (b)
d) none of the mentioned
Ans- a) by using the parameter variables $1, $2, $3.
Q – 17 What command would send the output of cmd1 to the input of cmd2?
a) cmd1 | cmd2
b) cmd1 || cmd2
c) cmd1 && cmd2
d) cmd1 ; cmd2
e) cmd1 cmd2
Ans- a) cmd1 | cmd2
Q – 18 Functions improves the shell’s program-ability significantly, because:
a) when we invoke a function, it is already in the shell’s memory, therefore a function runs faster than seperate scripts
b) function provides a piece of code for repetative tasks
c) both (a) and (b)
d) none of the mentioned
Ans- c) both (a) and (b)
Q – 19 The keyword ‘local’ is used:
a) to define a variable within a function for its local scope
b) to redefine any global variable
c) this is not a valid keyword
d) none of the mentioned
Ans- a) to define a variable within a function for its local scope
Q – 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
Ans- c) both (a) and (b)
Q – 22 ? Specifies:
a) one or more character
b) zero or more charecter
c) one character
d) None of the above
Ans- c) one character
Q – 23 * Specifies:
a) one or more character
b) zero or more charecter
c) nothing
d) None of the above
Ans- b) zero or more charecter
Q – 24 When the return value of any function is not specified within the function, what function returns?
a) nothing
b) exit status of the last command executed
c) 0
d) none of the mentioned
Ans- b) exit status of the last command executed
Q – 25 What is the output of the following command for bash shell:
echo linux $0
a) linux echo
b) linux linux
c) linux bash
d) linux
Ans- c) linux bash
Q – 26 Which of the following file set in the current directory are identified by the regular expression a?b*?
a) afcc, aabb
b) aabb, axbc
c) abbb, abxy
d) abcd, axbb
Ans- b) aabb, axbc
Q – 27 Which command can be used to test various file attributes:
a) if
b) file
c) test
d) type
Ans- c) test
Q – 28 The $ variables in a shell script context designates:
a) The runtime of the script
b) Number of command line arguments
c) PID of the shell running the script
d) The exit status of the shell script
Ans- c) PID of the shell running the script
Q – 29 Syntax to suppress the display of command error to monitor?
a) command > &2
b) command 2> &1
c) command 2> &2
d) command 2> /dev/null
Ans- d) command 2> /dev/null
Q – 32 Executing cat /etc/password > /dev/sda as superuser will:
a) Write data into a regular file called /dev/sda
b) Write data to the physical device sda
c) Create a temporary file /dev/sda and write data to it
d) None of the above
Ans- b) Write data to the physical device sda
Q – 33 cat < file1 >> file2 | file3:
a) file1 content will be appended to file2 and finally stored in file3
b) file1 content will be appended to file2 and file3 will be ignored
c) file2 and file3 will have same content
d) syntax error
Ans- d) syntax error
Q – 34 Which of these is the correct method for appending “foo” in /tmp/bar file?
a) echo foo > /tmp/bar
b) echo foo >> /tmp/bar
c) echo foo | /tmp/var
d) /tmp/bar < echo foo
Ans- b) echo foo >> /tmp/bar
Q – 35 From where would the read statement read if the following statements were executed?
exec < file1
exec < file2
exec < file3 read line a) It would read all the files b) It would not read any files c) It would read all the files in reverse order d) It would read only file3 Ans- b) It would not read any files Is This Answer Correct? 0 Yes 0 No Submit Your Answer Q – 36 The following commands gives the output like this: #cat file1 file2 #cat: file1: No such file or directory hello If we execute the command “cat file1 file2 1>2 2>&1” the output would be
a) cat: file1: No such file or directory hello
b) No output is displayed
c) Cat: 1>2: No such file or directory
d) hello
Ans- b) No output is displayed
Q – 37 cmd 2>&1 > abc will:
a) Write file2 to file1
b) Write standard output and standard error to abc
c) Write standard error to abc
d) Write standard output to abc & standard error to monitor
Ans- d) Write standard output to abc & standard error to monitor
Q – 38 cmd > abc 2>&1 will:
a) Write file2 to file1
b) Write standard output and standard error to abc
c) Write standard error to abc
d) Write standard output to abc & standard error to monitor
Ans- b) Write standard output and standard error to abc
Q – 39 The redirection 2> abc implies:
a) Write file 2 to file abc
b) Write standard output to abc
c) Write standard error to abc
d) none of the mentioned
Ans- c) Write standard error to abc
Q – 40 How to feed standard output of one command to standard input of another in a single shell session?
a) IO redirection can be used
b) Named pipes can be used
c) The pipe operator provided by the shell can be used
d) It can not be done
Ans- c) The pipe operator provided by the shell can be used
Q – 41 Tell me which of the following commands allows definition and assignment of environment variables under bash:
a) env
b) export
c) environ
d) setenviron
Ans- a) env
Q – 42 What would be the current working directory at the end of the following command sequence?
$ pwd
/home/user1/proj
$ cd src
$ cd generic
$ cd .
$ pwd
a) /home/user1/proj
b) /home/user1/proj/src
c) /home/user1
d) /home/user1/proj/src/generic
Ans- d) /home/user1/proj/src/generic
Q – 43 How do you print the lines between 5 and 10, both inclusive?
a) cat filename | head | tail -6
b) cat filename | head | tail -5
c) cat filename | tail +5 | head
d) cat filename | tail -5 | head -10
Ans- a) cat filename | head | tail -6
Q – 44 Which of these is not a valid variable in bash:
a) __ (double underscore)
b) _1var (underscore 1 var )
c) _var_ (underscore var underscore)
d) some-var (some hyphen var)
Ans- d) some-var (some hyphen var)
Q – 45 What is the return value ($?) of this code:
os = Unix
[$osName = UnixName] && exit 2
[${os}Name = UnixName] && exit 3
a) 0
b) 1
c) 2
d) 3
Ans- d) 3
Q – 46 What will be output of following command:
$ echo “The process id is” $$$$
a) The process id is $$
b) The process id is $$
c) The process id is
d) The process id is $$$$
Ans- c) The process id is
Q – 47 Create a new file “new.txt” that is a concatenation of “file1.txt” and “file2.txt”?
a) cp file.txt file2.txt new.txt
b) cat file1.txt file2.txt > new.txt
c) mv file[12].txt new.txt
d) ls file1.txt file2.txt | new.txt
Ans- b) cat file1.txt file2.txt > new.txt
Q – 48 What is the output of the following code:
os=Unix
echo 1.$os 2.”$os” 3.’$os’ 4.$os
a) 1.Unix 2.Unix 3.Unix 4.Unix
b) 1.Unix 2.Unix 3.$os 4.Unix
c) 1.Unix 2.Unix 3.Unix 4.$os
d) 1.Unix 2.$os 3.$os 4.$os
Ans- b) 1.Unix 2.Unix 3.$os 4.Unix
Q – 49 What is the output of the following program?
b =
[ -n $b ]
echo $?
[ -z $b ]
echo $?
a) 1
1
b) 2
2
c) 0
0
d) 0
1
Ans- c) 0
0
Q – 50 The expression expr -9 % 2 evaluates to:
a) 0
b) 1
c) -1
d) 2
Ans- c) -1
Q – 51 What is the output of the following program?
x = 3; y = 5; z = 10;
if [( $x -eq 3 ) -a ( $y -eq 5 -o $z -eq 10 )]
then
echo $x
else
echo $y
fi
a) 1
b) 3
c) 5
d) Error
Ans- b) 3
Q – 52 What is the output of the following program?
[ -n $HOME ]
echo $?
[ -z $HOME ]
echo $?
a) 0
1
b) 1
0
c) 0
0
d) 1
1
Ans- a) 0
1
Q – 53 The statement z = ‘expr 5 / 2′ would store which of the following values in z?
a) 0
b) 1
c) 2
d) 2.5
e) 3
Ans- c) 2