Unix Questions and Answers – Command Substitution and Shell Variables

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

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

Answer: a
Explanation: 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 (‘).
advertisement
advertisement

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

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

Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

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

advertisement

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

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

Answer: c
Explanation: 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) @
View Answer

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

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

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

$ unset  x

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

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

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

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

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

pwd=/home/usr10/doc

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

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

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

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

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

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of 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.