250+ TOP MCQs on Shell Variables and Answers

Unix Multiple Choice Questions on “Shell Variables”.

1. The UNIX shell is both _______ and _______ language.
a) interactive, responsive
b) interpreter, executing
c) scripting, interpreter
d) high level, low level

Answer: c
Clarification: The UNIX shell is both an interpreter and scripting language. We can also say that shell can be interactive or non-interactive. When we log in to our system, interactive shell presents a prompt and wait for our requests while a non-interactive shell is managed by an interactive shell while executing a shell script.

2. Which of the following function(s) are performed by an interactive shell?
a) job control
b) history
c) aliases
d) job control, history, aliases

Answer: d
Clarification: When we log in to our system, an interactive shell presents a prompt and waits for our requests. This type of shell supports functions like job control, history, aliases etc.

3. Every feature used in an interactive shell can also be used by a shell script.
a) True
b) False

Answer: b
Clarification: An interactive shell runs a non-interactive shell while executing a shell script. Every feature used in a shell script can also be used in an interactive shell but the reverse is not true. Job control and history have no meaning in a shell script.

4. Which one of the following is arguably the best shell to use?
a) Bash
b) Korn
c) C
d) Bourne

Answer: a
Clarification: Bash was developed by GNU as a bourne again shell. It was a grand superset in that that contains the features of both C and Korn shells. Bash was developed to ultimately conform to the POSIX shell specification and is arguably the best shell to use.

5. Which one of the following command is used to create a child shell?
a) fork
b) wait
c) sh
d) env

Answer: c
Clarification: To create a child shell we can use the sh command. sh is basically the command name of Bourne shell. sh is a command language interpreter that executes commands read from a command line string. To create a child shell, simply invoke sh command without any argument.

6. Shell variables are of ____ types.
a) 1
b) 3
c) 2
d) 4

Answer: c
Clarification: Shell variables are of two types –local and environment. For example, PATH, HOME and SHELL are environment variables. They are called so because they are available in the user’s total environment whereas local variables are more restricted in scope.

7. Suppose ABD_DIR is a local variable. Then it will be accessible to the child process.
a) True
b) False

Answer: b
Clarification: Since ABD_DIR is a local variable and local variables are more restricted to the scope so it will not be accessible to the child process. For example,

$ ABD_DIR=/home/users/abd/doc       // local variable in parent process
$echo ABD_DIR
home/users/abd/doc
$ sh                              // create a child shell
$echo ABD_DIR
_                                // variable not available in child shell

8. ____ command is used to display the environment variables only.
a) set
b) env
c) sh
d) var

Answer: b
Clarification: env command is used to display environment variables only in the current shell. For example, invocation of env command will produce an output in the following form:

$ env
HOME=/home/abd
LOGNAME=abd
MAIL=/var/mail/kumar
SHELL=/usr/bin/bash
TERM=xterm

9. ____ command displays all the variables available in the current shell.
a) env
b) set
c) var
d) sh

Answer: b
Clarification: set command is used to display all the variables available in the current shell. set is a built-in command. env is an external command and runs in a child process. It thus displays only those variables that are inherited from its parent, the shell.

10. Environment variable names can be defined only in uppercase.
a) True
b) False

Answer: b
Clarification: We’ve seen that environment variables are generally defined in uppercase only. But there is nothing which can prevent us from using a different scheme.

11. Environment variables control the behavior of the system.
a) True
b) False

Answer: a
Clarification: Environment variables control the behavior of the system. They determine the environment in which we work. If they are not set properly, we may not be able to use some of the commands without a pathname.

12. The shell has ____ prompts.
a) 1
b) 4
c) many
d) 2

Answer: d
Clarification: The shell has two prompts (PS1 and PS2). The primary prompt string PS1 is the one we normally see i.e. ($). The > is the secondary prompt stored in PS2 which is used by the shell while responding to a multiline command.

13. Which one of the following command will change our primary prompt from $ to C>?
a) PS1=”C> “
b) PS2=”C>”
c) PS1=”>C”
d) PS2=”<C”

Answer: a
Clarification: The primary prompt string PS1 is the one we normally see i.e. $. But we can change this prompt for our convenience. As we know that the primary prompt string is stored in PS1, we can use the following command to change our prompt.

$ PS1=”C>”
C>        // new prompt

14. Which environment variable is used to display our username?
a) PATH
b) MAIL
c) LOGNAME
d) HOME

Answer: c
Clarification: LOGNAME is an environment variable which can be used with echo command as an argument to display our username. We can also know our username using who am i command. For example,

$ echo $LOGNAME    
Abd                // username

15. Which one of the following is not an environment variable?
a) HOME
b) PATH
c) USER
d) env

Answer: d
Clarification: env is an external command which runs in a child process and it lists only environment variables which are inherited from its parent, the shell.

VARIABLES    SIGNIFICANCE
HOME-         home directory, the directory where a user is placed on logging in
USER-         login name of user
PATH-         list of directories searched by shell to locate the command

Leave a Reply

Your email address will not be published.