Question

Difficulty: MediumBasic Scripting Languages and Constructs

A Linux administrator is troubleshooting an automated system maintenance script saved with a `.sh` file extension. The script needs to verify whether a destination directory stored in an environment variable named `TARGET_PATH` exists before initiating a file copy operation. However, the script generates a syntax error at execution. Which of the following represents the correct syntax to reference this environment variable in a Linux Bash shell script?

  1. $TARGET_PATHAnswer
  2. B
    %TARGET_PATH%
  3. C
    $env:TARGET_PATH
  4. D
    var TARGET_PATH

Answer

The syntax $TARGET_PATH correctly references environment variables within a Linux Bash shell script.
In Linux Bash shell scripts (`.sh`), environment variables are accessed by prefixing the variable identifier with a dollar sign (`),suchas`), such as ` TARGET_PATH` or `${TARGET_PATH}`.

Step-by-Step Solution

1
Identify the scripting environment based on the file extension.
The .sh extension indicates a Unix/Linux shell script running under an environment such as Bash.
Different scripting runtimes use distinct syntaxes for referencing environment variables.
2
Evaluate the correct variable referencing rule for Bash.
In Bash shell scripting, variables are expanded by prefixing the variable name with a dollar sign (),suchas), such as TARGET_PATH or ${TARGET_PATH}.
Prefixing with $ signals the interpreter to evaluate the value stored in the environment variable.

Key Concept

Environment Variable Syntax in Shell Scripts
Estimated Time:1m 0s
Rate this question