A junior administrator writes the following line in a Bash script to capture the first log file created on the current date that is stored inside a compressed archive:
BACKUP_DIR=`tar -tzf backup.tar.gz | grep "\`date +%F\`" | head -1`
The script fails with a "command not found" error because the inner backticks are parsed too early. Which rewritten command follows POSIX-compliant best practice for nested command substitution and eliminates the escaping problem?
BACKUP_DIR=\((tar -tzf backup.tar.gz | grep "\)(date +%F)" | head -1)
BACKUP_DIR=tar -tzf backup.tar.gz | grep "$(date +%F)" | head -1
The modern, POSIX-compliant form of command substitution is $( … ). It may be freely nested and does not require the awkward backslash-escaping that backticks need. Rewriting the command so that both the outer substitution and the inner date command use $( … ) resolves the parsing error and greatly improves readability:
BACKUP_DIR=$(tar -tzf backup.tar.gz | grep "$(date +%F)" | head -1)
Using backticks on the outside still leaves the legacy syntax in place, and mixing quoting with backticks provides no advantage. Keeping backticks inside the $( … ) form (or vice-versa) re-introduces the same nesting problem. Adding an extra pipeline with tr -d '\n' does not fix the underlying quoting issue and alters the data unexpectedly. Therefore, the variant that converts all substitutions to $( … ) and keeps the inner date expression inside double quotes is the only answer that meets best-practice guidance and works in any modern POSIX shell.
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.
Why is $(...) preferred over backticks in POSIX scripts?
Open an interactive chat with Bash
What does the command 'tar -tzf' do in this script?
Open an interactive chat with Bash
Why is '$(date +%F)' used in the script?
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