Which THREE of the following practices are recommended when creating simple shell scripts in a Red Hat Enterprise Linux environment to ensure reliability, security, and maintainability?
Trap 1: Use #!/bin/sh for compatibility, even if bash-specific features are…
The shebang line must name the interpreter that actually executes the script; on modern Debian/Ubuntu systems /bin/sh is dash, a POSIX-only shell lacking bash arrays, [[ ]], and process substitution. Declaring #!/bin/sh while using bashisms leads to runtime syntax errors or unexpected behavior. For compatibility with bash features, explicitly use #!/bin/bash or, better, write POSIX-compliant code if portability is required.
Trap 2: Always run scripts by invoking the interpreter directly (e.g., bash…
Running a script with bash script.sh works, but it skips the shebang and negates the purpose of making the file executable, which is required for it to be invoked by name from PATH with ./script.sh. The executable bit and shebang together allow the kernel to select the correct interpreter automatically, and they are the standard for distributing scripts. Requiring users to know the interpreter reduces portability and convenience.
- A
Use #!/bin/sh for compatibility, even if bash-specific features are needed.
Why wrong: The shebang line must name the interpreter that actually executes the script; on modern Debian/Ubuntu systems /bin/sh is dash, a POSIX-only shell lacking bash arrays, [[ ]], and process substitution. Declaring #!/bin/sh while using bashisms leads to runtime syntax errors or unexpected behavior. For compatibility with bash features, explicitly use #!/bin/bash or, better, write POSIX-compliant code if portability is required.
- B
Quote variables when used in commands, e.g., "$file" instead of $file.
Unquoted variable expansions are subject to word splitting and pathname expansion, so a filename like 'My File.txt' would be split into two arguments, and a value containing '*' could expand to multiple files. Quoting, as in "$file", preserves the variable's value as one literal argument, preventing these transformations. This is essential when handling user input, file paths, or any value with whitespace or glob characters.
- C
Start the script with a shebang line, e.g., #!/bin/bash.
A shebang line, such as #!/bin/bash, tells the operating system kernel which interpreter to invoke when the script is executed directly, and it must be the first line of the file. Without it, execution may fail with 'permission denied' or fall back to the current shell, which may not support the script's syntax. Choosing the correct shebang ensures the script runs in a predictable environment and that bash-specific features are available.
- D
Include set -e at the beginning of the script to exit on any error.
set -e makes the script abort immediately when any command or simple pipeline returns a non-zero exit status, so errors do not silently cascade. This is a common reliability safeguard, but it has caveats: it is ignored in conditional contexts like if or while tests, and commands whose failure is expected must be explicitly handled. Used thoughtfully, it improves deterministic behavior and simplifies debugging.
- E
Always run scripts by invoking the interpreter directly (e.g., bash script.sh) instead of making them executable.
Why wrong: Running a script with bash script.sh works, but it skips the shebang and negates the purpose of making the file executable, which is required for it to be invoked by name from PATH with ./script.sh. The executable bit and shebang together allow the kernel to select the correct interpreter automatically, and they are the standard for distributing scripts. Requiring users to know the interpreter reduces portability and convenience.