250+ TOP MCQs on Command Substitution and Shell Variables

Unix Multiple Choice Questions & Answers (MCQs) on “Command Substitution and Shell Variables”.

1. Shell enables one or more arguments to be obtained from the standard output of another command. This feature is called _________
a) command substitution
b) argument substitution
c) shell substitution
d) korn

Answer: a
Clarification: Apart from a pipeline, shell enables connecting of two commands in another way. Shell enables one or more arguments to be obtained from the standard output of another command. This feature is called command substitution. For example, to display output like:

Today’s date is Sat  Jan 05 17:25:41  IST 2002
We can use the following command
$ echo “Today’s date is `date`”        // date is an argument to echo command

2. Which of the following meta-character is used in command substitution?
a) `
b) ‘
c) “
d) >

Answer: a
Clarification: When scanning the command line, the shell looks for another metacharacter i.e. ` (the backquote) usually placed on the top-left of our keyboard, and it should not be confused with a single quote (‘).

3. Command substitution is enabled in single quotes also.
a) True
b) False

Answer: b
Clarification: Command substitution is enabled only when we use double quotes. If we use single quotes, it will not work. For example,

$ echo ‘today’s date is `date` ‘
Output will be: today’s date is `date
$ echo “today’s date is `date` “
Output will be: today’s date is  Sat  Jan 05 17:25:41  IST 2017

4. POSIX recommends the use of ____ instead of archaic `command ` for command substitution.
a) |
b) #
c) %
d) $

Answer: d
Clarification: POSIX recommends the use of the form $(command) instead of archaic `command` for command substitution. For example, to display the date using command substitution use the following command:

$ echo The date is $(date)
Output: The date is  Sat  Jan 05 17:25:41  IST 2017

5. Which of the following shell doesn’t support the command substitution using $ recommended by POSIX?
a) Korn
b) bash
c) C
d) bourne

Answer: d
Clarification: Whether or not we use POSIX notation for command substitution is something up to the user. But we should make sure that we do not have to run our shell scripts with the Bourne shell because $(command) is not supported by Bourne shell.

6. Which symbol is used for assigning a value to variables?
a) $
b) &
c) =
d) @

Answer: c
Clarification: The shell supports variables that are useful both in the command line and shell scripts. These variables are called shell variables like pwd. A variable assignment is of the form variable=value(no spaces around =). For example,

$ count=10        // a variable named count is assigned a value of 10

7. Which symbol is used for evaluation of variables?
a) $
b) &
c) =
d) @

Answer: a
Clarification: The shell supports variables that are useful both in the command line and shell scripts. These variables are called shell variables like TERM and SHELL. A variable assignment is of the form variable=value(no spaces around =), but its evaluation requires the $ as a prefix to the variable name. For example,

$ count=10
$ echo $count        // output will be 10

8. Which of the following is a correct initialization of variables to null strings?
a) x=
b) x=’ ‘
c) x=” “
d) x=, x=’ ‘, x=” “

Answer: d
Clarification: All shell variables are initialized to null strings by a string. While explicit assignment of null strings can be performed with x=’ ‘ or x=” “ or x=

9. A variable can be removed using _____
a) unset
b) readonly
c) del
d) bash

Answer: a
Clarification: A variable can be removed using the unset command. unset in an internal command. For example, if we want to undefine a variable x then,

10. readonly command is used to protect a variable from reassignment.
a) True
b) False

Answer: a
Clarification: A variable can be protected from reassignment by readonly command. unset is also an internal command. For example, to protect a variable x from reassignment use the following command,

$ readonly  x            // x can’t be reassigned now

11. C shell uses which command for assigning values to variables?
a) =
b) set
c) unset
d) $

Answer: b
Clarification: The C shell uses the set statement to set variables. There is a restriction that there either has to be whitespace on both sides of = or none at all. For example, for assigning a value to a variable named count, use the following command:

$ set count=1 
Or 
$ set count=1

12. The variable assignment as x = 10 (whitespace on both sides of =) will work if we are not using C shell?
a) True
b) False

Answer: b
Clarification: If we are using any other shell other than C shell, the assignment in the form x = 10 will produce an error because the shell will interpret x as a command and =,10 as its arguments.

13. What will the result when we evaluate this statement?

$ directory=’pwd’=`pwd`

a) output of pwd command along with string pwd=
b) undefined output
c) erroneous
d) directory variable will hold string pwd

Answer: a
Clarification: In the above statement, the string pwd, = and the output of pwd command are concatenated and saved in the directory variable. So the above statement will evaluate the current working directory as:

14. Which of the following is not a system defined variable?
a) $PATH
b) $HOME
c) $SHELL
d) $cd

Answer: d
Clarification: PATH is the shell variable which stores the list of directories that the shell searches for locating commands. HOME prints our home directory while SHELL prints the absolute pathname of our login shell. cd is a command which is used for changing directories or moving through file hierarchy.

15. Which of the following is an invalid variable?
a) _user
b) us01
c) -txtfile
d) txt123

Answer: c
Clarification: UNIX restricts some rule for defining a variable. A variable name must begin with either an underscore (_) or alphanumeric character followed by one or more alphanumeric or underscore characters.

Following are some valid variable names:
User01        user_01    _user67
Following are some invalid variable names:
2_var        user!        -textfile

16. Command substitution requires the command to use ________
a) standard input
b) standard output
c) standard error
d) all of the mentioned

Answer: b
Clarification: The shell enables one or more command arguments to be obtained from the standard output of another command. This feature is called command substitution and it requires the command to use the standard output (stream).

17. The command is valid.

$ ls  -lRa  $HOME  > home.ls

a) True
b) False

Answer: a
Clarification: Above command will save the entire home directory structure including the hidden files in a file named home.ls because we have used -l, -a, -R, all the files including hidden ones are recursively saved in a separate file named home.

Leave a Reply

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