A system administrator is writing a script that creates a daily backup. The script must use the path from the BACKUP_TARGET environment variable. If BACKUP_TARGET is unset or empty, the script should use /var/backups as the target for that command only, without permanently changing the variable. Which of the following commands correctly achieves this?
The correct syntax for using a default value for an unset or null variable for a single operation is ${parameter:-word}. This form of parameter expansion substitutes the word if the parameter is unset or null, but does not alter the state of the variable itself.
The command cp /var/log/app.log "${BACKUP_TARGET:-/var/backups}/app-$(date +%F).log" correctly uses this syntax.
The syntax ${parameter:=word} would also use the default value, but it would incorrectly assign /var/backups to the BACKUP_TARGET variable if it was unset or null, which is against the requirements.
Using $() executes a command substitution, which is incorrect for this purpose as it attempts to execute the contents as a command.
Using () creates a subshell, which is also incorrect syntax for providing a default value to a variable within a string.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is parameter expansion in Linux?
Open an interactive chat with Bash
Why is `${parameter:-word}` preferred over `${parameter:=word}` in the given script?
Open an interactive chat with Bash
What is the difference between `${parameter:-word}` and `()` or `$()` in this context?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access