250+ TOP MCQs on Shell Programming and Answers

Linux / Unix questions and answers focuses on Bash Shell programming. It will be useful for both fresher & experienced who are learning Linux Basics, Essentials and Shell programming.

1. What will be output of following command:

$ echo "The process id is" $$$$
 
a) The process id is $$
b) The process id is $<pid>$<pid>
c) The process id is <pid><pid>
d) The process id is $$$$

View Answer

Answer: c
Clarification: None.

 
 

2. 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
Answer: d
Clarification: None.

3. 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
Answer: a
Clarification: None.

4. 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
Answer: b
Clarification: None.

5. 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)
Answer: d
Clarification: None.

6. 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
Answer: b
Clarification: None.

7. 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
Answer: d
Clarification: None.

8. 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
Answer: b
Clarification: None.

9. 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

View Answer

Answer: a
Clarification: None.

 
 

10. 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

View Answer

Answer: c
Clarification: None.

 
 

11. The expression expr -9 % 2 evaluates to:
a) 0
b) 1
c) -1
d) 2
Answer: c
Clarification: None.

12. The statement z = ‘expr 5 / 2’ would store which of the following values in z?
a) 0
b) 1
c) 2
d) 2.5
Answer: c
Clarification: None.

Leave a Reply

Your email address will not be published. Required fields are marked *