XK0-004 · Question #434
An administrator is writing a Bash script that contains a loop that increments the value of the variable count with each loop iteration by 1. Which of the following should the administrator use?
The correct answer is A. $count++. In Bash arithmetic contexts, the post-increment operator increments a variable by 1 after its current value is used, making $count++ the intended increment syntax for the loop.
Question
An administrator is writing a Bash script that contains a loop that increments the value of the variable count with each loop iteration by 1. Which of the following should the administrator use?
Options
- A$count++
- BCount='expr $count+1'
- CCount = $conunt+1
- D((++count))
How the community answered
(28 responses)- A93% (26)
- B4% (1)
- D4% (1)
Why each option
In Bash arithmetic contexts, the post-increment operator increments a variable by 1 after its current value is used, making $count++ the intended increment syntax for the loop.
The $count++ expression applies the C-style post-increment operator, which is recognized inside Bash arithmetic evaluation constructs such as (( $count++ )) or $(( $count++ )), incrementing the variable by 1 each iteration. It is the most concise and readable form matching standard increment notation used in many scripting and programming contexts. Among the provided choices, it correctly represents the post-increment pattern the question describes.
Single quotes suppress all variable expansion and command substitution in Bash, so Count='expr $count+1' assigns the literal string 'expr $count+1' to Count rather than computing a numeric result.
Bash treats spaces around '=' as argument separators outside arithmetic context, so 'Count = $count+1' is parsed as a command named 'Count' with '=' and '$count+1' as arguments, not an assignment.
While ((++count)) is valid Bash arithmetic syntax, it uses the pre-increment form which increments before returning the value, and the notation differs from the post-increment pattern referenced by the correct answer.
Concept tested: Bash variable increment operator syntax in scripts
Source: https://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic
Topics
Community Discussion
No community discussion yet for this question.