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
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?
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 (‘).
3. Command substitution is enabled in single quotes also.
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
4. POSIX recommends the use of ____ instead of archaic `command ` for command substitution.
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:
$ 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?
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?
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?
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?
b) x=’ ‘
c) x=” “
d) x=, x=’ ‘, x=” “
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 _____
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.
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?
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?
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?
a) output of pwd command along with string pwd=
b) undefined output
d) directory variable will hold string pwd
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:
14. Which of the following is not a system defined variable?
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?
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
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
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.