250+ TOP MCQs on Shell Variables and Answers

Unix Question Paper on “Shell Variables”.

1. Which symbol is used for setting the PS1 prompt to show the current event number?
a) ^
b) !
c) &
d) |

Answer: b
Clarification: Bash and Korn shell also supports history facility that treats a previous command as an event and associates it with a number. Thus we can recall and execute previous commands by their event numbers. Using the !, we can set the PS1 prompt to show the current event number. For example,

$ PS1=’ [!] ’        //bash requires  before !
[42] _              // number associated 
Every time a command is executed, the event number is incremented.

2. Which escape sequence is used with PS1 to show the hostname of our computer?
a) n
b) c
c) h
d) e

Answer: c
Clarification: Bash uses a number of escape sequences to make our prompt as much information as possible. For example, the h string will show the hostname of our computer.

$ PS1=”h ”
Saturn> _        //Saturn is the machine’s name

3. Which of the following shells support the use of aliases?
a) bourne
b) Korn
c) bash
d) Korn and bash

Answer: d
Clarification: Bash and Korn shell supports the use of aliases which lets us assign shorthand names to frequently used commands. For example,

$ alias  a1=’ls  -l’    // quoting necessary for multiple words
$ a1                   // a1 will now execute the ls  -l command

4. We can display an alias definition by using an alias with the name.
a) True
b) False

Answer: a
Clarification: Aliases lets us assign shorthand names to frequently used commands. We can display an alias definition using an alias with the name. For example,

$ alias  a1
a1=’ls  -l’

5. Which command is used in bash and Korn shells to display the previously used commands?
a) his
b) history
c) sh
d) ps

Answer: b
Clarification: Bash and Korn support a versatile history feature that treats a previous command as an event and associates it with an event number. The history command displays the history list showing the event number of every previously executed command.

6. To display the last five used commands, which one of the following commands is used (in bash shell)?
a) history 5
b) history -5
c) history
d) history 5-

Answer: a
Clarification: While bash displays the complete history list using history command. Korn lists the 16 commands recently used. But we can easily find out the last five commands by using a numeric argument:

$ history  5        // in bash
$ history -5        // in Korn

7. Which of the following symbols are used for accessing previous commands by event numbers?
a) !
b) r
c) $
d) ! and r

Answer: d
Clarification: The ! symbol (r in Korn) is used to repeat previous commands. For repeating the last command we have to use !! in bash and r in Korn. We can repeat other commands also, but with the event number as an argument. For example, following commands will repeat the command with event number 38,

$ !38      // in bash 
$ r 38     // in Korn

8. We can use relative addressing while executing previous commands.
a) True
b) False

Answer: a
Clarification: Suppose if we don’t know the event number and we want to execute the command prior to the previous one, we can use relative addressing by using a negative argument with ! or r. For example,

$ !-2         //in bash
$ r -2        //in Korn

9. We can also execute previous commands by context.
a) True
b) False

Answer: a
Clarification: It may happen that we don’t remember the event numbers of the commands except for the immediately preceding two or three, but we remember that the command started with a specific letter or string. In such a case, we can execute previous commands by context by using ‘v’ along with ! or r. For example,

$ !v        // repeat last command beginning with v (in bash)
$ rv        // repeats last command beginning with v (in Korn)

10. Which symbol is used as a shorthand for using the last argument to the previous command?
a) %
b) _
c) |
d) $

Answer: d
Clarification: Sometimes we run several commands on the same file and instead of specifying the filename, again and again, we can use $ as a shorthand. This expression signifies the last argument to the previous command. For example,

$ mkdir prog_one       // create a directory 
$ cd $                 // change directory to mkdir

Leave a Reply

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