010-160 practice question 5 of 15
The file script.sh in the current directory contains the following content: #!/bin/bash echo $MYVAR The following commands are used to execute this script: MYVAR=value ./script.sh The result is an empty line instead of the content of the variable MYVAR. How should MYVAR be set in order to make script.sh display the content of MYVAR?
Full set: 81 questionsBy default, variables defined in a shell are local to that shell session. When a new process, such as the script.sh script, is launched, it does not inherit the parent shell's local variables. The export command marks a variable to be passed to the environment of child processes. By using export MYVAR=value, the variable MYVAR becomes an environment variable, which is then inherited by the script.sh process, allowing the script to access its value and display it correctly.
A. !MYVAR=value: The ! character is used for history expansion or logical negation in shell tests; it is not a valid syntax for variable assignment.
B. env MYVAR=value: The env command runs a program in a modified environment but does not set the variable for subsequent commands in the current shell.
C. MYVAR=value: This creates a local shell variable, which is not passed to child processes. This is the behavior described in the problem statement.
D. $MYVAR=value: The $ prefix is used to retrieve the value of a variable (expansion), not for assignment. This syntax is incorrect.
1. GNU Bash Reference Manual, Section 3.2.1 Shell Variables: "Variables are introduced into the environment of child processes using the export builtin."
2. GNU Bash Reference Manual, Section 4.1 Bourne Shell Builtins, export: "Mark each name to be passed to child processes in the environment. If -f is given, the names refer to functions. If no names are given, or if -p is given, a list of all names that are exported in this shell is printed."
3. Shotts, W. (2019). The Linux Command Line: A Complete Introduction (2nd ed.). No Starch Press. Chapter 7, "Environment Variables", p. 88: "To see how this works, let’s try an experiment. We will define a variable in our shell and then try to view it from a new shell instance started from our original shell... The variable foo does not appear. This is because foo is a shell variable and is local to the shell in which it was defined. To share a variable with a child process, we must convert it into an environment variable using the export command."
4. MIT OpenCourseWare, "The Missing Semester of Your CS Education", Lecture 2: Shell Tools and Scripting (2020), Environment Variables section: "When a program is executed, it inherits the environment variables from its parent process. You can set environment variables for a single command by prepending the assignment... To set an environment variable for the current shell and all its children, use the export command."
Keep practicing
Take the free 010-160 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 010-160 practice questions
- Question 1What can be found in the /proc/ directory?
- Question 2Which permissions are set on a regular file once the permissions have been modified with …
- Question 3Which of the following commands puts the lines of the file data.csv into alphabetical ord…
- Question 4Which of the following programs are web servers? (Choose two.)
- Question 6Reverse DNS assigns hostnames to IP addresses. How is the name of the IP address 198.51.1…
- Question 7The current directory contains the following file: -rw-r—r— 1 root exec 24551 Apr 2 12:36…
