Unix Questions and Answers – Shell Variables – 2

This set of Unix Question Paper focuses on “Shell Variables – 2”.

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

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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.

advertisement
advertisement
$ 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
View Answer

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

Note: Join free Sanfoundry classes at Telegram or Youtube
$ 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
View Answer

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

advertisement
$ 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
View Answer

Answer: b
Explanation: 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.
advertisement

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-
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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) $
View Answer

Answer: d
Explanation: 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

Sanfoundry Global Education & Learning Series – Unix.

To practice all questions papers on Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.