Questão de Linux Administration #019e3987
After issuing:
function myfunction { echo 12 ; }
in Bash, which output does:
myfunction A B C
Produce?
A B
A B C
A C
B C
C B A
Explicação Oficial / Explicações da Comunidade
Summary
This question tests understanding of how Bash functions handle positional parameters. When a function is defined with function myfunction { echo $1 $2 ; }, it only references the first two arguments ($1 and $2). Calling myfunction A B C passes three arguments, but the function ignores $3. Thus, it outputs A B.
How it Works
In Bash, functions use positional parameters $1, $2, ..., $N to access arguments passed to them. $0 is the function name (or script name). The $@ and $* variables represent all arguments. When a function is invoked, the arguments are assigned to these parameters. The function body executes with those parameters set. If the function does not reference a parameter beyond the ones passed, those extra arguments are simply ignored.
Why the correct answer is right
Option A (A B) is correct because the function echoes $1 (first argument, A) and $2 (second argument, B). The third argument C is not used.
Why incorrect options are wrong
- B. A B C: Incorrect because the function only echoes
$1and$2, not$3. - C. A C: Incorrect because
$2isB, notC. The function does not skip$2. - D. B C: Incorrect because
$1isA, notB. - E. C B A: Incorrect because the order is reversed and includes
Cwhich is not referenced.