A backup-rotation script stores the number of days to keep archives in the variable RETENTION_DAYS. The script should print a warning only when the value is greater than 30, but the existing test sometimes triggers even when the value is 5:
if [[ $RETENTION_DAYS > 30 ]]; then
echo "Warning: retention limit exceeded"
fi
Which single-line replacement correctly performs an integer comparison and prevents side effects such as unintended string comparison or redirection?
Within the test or [[ … ]] compound command, -gt is the numeric "greater than" operator; it converts its operands to integers and returns success only when the left-hand value is numerically larger. Using > inside [[ … ]] performs a lexicographical string comparison, so "5" is considered greater than "30"-hence the false warnings. In (( … )) arithmetic context the shell expects C-style operators such as >, and the token -gt causes a syntax error. Omitting the hyphen ("gt") is simply an invalid operator. Therefore the statement that reliably tests whether RETENTION_DAYS is numerically greater than 30 is:
if [[ $RETENTION_DAYS -gt 30 ]]; then
This form evaluates correctly for any integer value and works in both POSIX [ … ] and Bash [[ … ]] syntax.
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 the difference between `-gt` and `>` in Bash comparisons?
Open an interactive chat with Bash
What is the purpose of `(( ... ))` in Bash scripting?
Open an interactive chat with Bash
Why is `[[ ... ]]` preferred over `[ ... ]` in modern Bash scripting?
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