This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Shell Variables – 1”.
1. The UNIX shell is both _______ and _______ language.
a) interactive, responsive
b) interpreter, executing
c) scripting, interpreter
d) high level, low level
Explanation: 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
d) job control, history, aliases
Explanation: 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.
Explanation: 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?
Explanation: 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?
Explanation: 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.
Explanation: 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.
Explanation: 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.
Explanation: 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.
Explanation: 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.
Explanation: 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.
Explanation: 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.
Explanation: 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> “
Explanation: 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?
Explanation: 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?
Explanation: 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
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.