ACE-P-ALE1-04 practice question 6 of 35

When scripting in bash, it is recommended to always use which symbols around your variables to reference them?

Full set: 100 questions
Answer options
A: {}
B: []
C: ()
D: ‘ ’
E: “ ” Correct answer
Correct Answer:
E. “ ”
Explanation

In bash scripting, it is a critical best practice to enclose variable references in double quotes (" "). This prevents unintended word splitting and filename expansion (globbing) by the shell. If a variable contains spaces, tabs, or newlines, referencing it without quotes (e.g., $VAR) would cause the shell to treat each word as a separate argument. Using double quotes (e.g., "$VAR") ensures the entire value of the variable is treated as a single string, preserving its integrity and preventing common, hard-to-debug errors.

Why Incorrect

A. {}: These are used for parameter expansion to disambiguate a variable name from adjacent text (e.g., ${VAR}name), not to prevent word splitting.
B. []: These are used for test constructs (e.g., if [ "$a" -eq "$b" ]) or array indexing, not for general variable referencing.
C. (): These are used to execute commands in a subshell or for array initialization, not for referencing a variable's value.
D. ‘ ’: Single quotes prevent all expansions. Referencing a variable within single quotes (e.g., '$VAR') will result in the literal string $VAR, not the variable's value. ---

References

1. GNU Bash Reference Manual, Section 3.5.3 Word Splitting: "The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. ... If a variable is not enclosed in double quotes, the value is subject to word splitting." This official manual for the bash shell explicitly states that double quotes are the mechanism to prevent this behavior.

Source: Free Software Foundation, Inc., Bash Reference Manual, Version 5.2, Section 3.5.3.

2. MIT, "The Missing Semester of Your CS Education", Lecture 6: The Shell: In the "Shell Scripting" section, the course material advises: "You should always quote your variables (e.g. "$foo"). See this post for why." The linked post details issues with word splitting and globbing that are solved by quoting.

Source: Anish Athalye, Jon Gjengset, and Jose Javier Gonzalez Ortiz, The Missing Semester of Your CS Education, Massachusetts Institute of Technology (MIT), IAP 2020, Lecture 6.

3. Arista EOS Central, "Introduction to Bash Scripting": While not a formal manual, Arista's own technical community resources demonstrate this best practice. In examples involving variables that might contain spaces, such as file paths or command output, the standard practice shown is to use double quotes. For instance, when capturing output: VAR="$(command)", the quoting is implicit in the examples that follow using $VAR.

Source: Arista Networks, EOS Central (Arista Community Forum), various scripting examples. (Note: This points to common practice within the vendor's ecosystem, which is relevant context for an Arista exam).

Scroll to Top