Linux Shell Programming Questions and Answers on Variables for Freshers
https://www.computersprofessor.com/2018/07/linux-shell-programming-questions-and_24.html
1. In the shell, by default, all variables are considered and stored as
a) string
b) integer
c) character
d) float
Answer: a
2. Which command reads user input from the terminal and assign this value to a variable name?
a) read
b) get
c) declare
d) set
Answer: a
3. 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
Answer: d
4. Which one of the following is not a valid shell variable?
a) _san
b) san_2
c) _san_2
d) 2_san
Answer: d
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.
5. 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
Answer: a
6. What is the output of this program?
#!/bin/bashsan_var="Sanfoundry"
echo "$san_var"
echo '$san_var'
echo '"$san_var"'
echo "'$san_var'"
echo \$san_var
exit 0
a) Sanfoundry
$san_var
“$san_var”
‘Sanfoundry’
$san_var
b) Sanfoundry
Sanfoundry
“Sanfoundry”
‘Sanfoundry’
Sanfoundry
c) program will generate an error message
d) program will print nothing
$san_var
“$san_var”
‘Sanfoundry’
$san_var
b) Sanfoundry
Sanfoundry
“Sanfoundry”
‘Sanfoundry’
Sanfoundry
c) program will generate an error message
d) program will print nothing
Answer: a
Explanation: Using double quotes does not affect the substitution of the variable, while single quotes and backslash do.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
Sanfoundry
$san_var
“$san_var”
‘Sanfoundry’
$san_var
root@ubuntu:/home/sanfoundry#
7. What is the output of this program?
#!/bin/bashvar1=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) program will print 10 & 20
b) program will generate a warning message
c) program will print 20
d) program will print 10 & 20
Answer: d
Explanation: The doller sign ($) is used to access a variable’s value, not to define it.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
./test.sh: line 3: 10=20: command not found
10
root@ubuntu:/home/sanfoundry#
8. What is the output of this program?
#!/bin/bashvar[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
b) san_2
c) san_3
d) san_1 san_2 san_3
Answer: d
Explanation: All items of an array can be accessed by using ${[*]} or ${[@]}.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
san_1 san_2 san_3
root@ubuntu:/home/sanfoundry#
9. What is the output of this program?
#!/bin/bashsan_var=helloreadonly san_varsan_var=hiecho $san_var
exit 0
a) hello
b) hi
c) nothing will print
d) none of the mentioned
b) hi
c) nothing will print
d) none of the mentioned
Answer: a
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/sanfoundry# ./test.sh
./test.sh: line 4: san_var: readonly variable
hello
root@ubuntu:/home/sanfoundry#
10. What is the output of this program?
#!/bin/bashsan_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
b) the value of is 10
c) the value of san_var is $san_var
d) the value of “san_var” is $san_var
Answer: a
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
the value of “san_var” is 10
root@ubuntu:/home/sanfoundry#
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
the value of “san_var” is 10
root@ubuntu:/home/sanfoundry#
