Linux Administration
medium

Questão de Linux Administration #019e3987

Enunciado da Questão
Linux Administration
#function
#echo
#positional parameters
#bash
Médio

After issuing:

function myfunction { echo 2 ; }

in Bash, which output does:

myfunction A B C

Produce?

A

A B

Correta
B

A B C

C

A C

D

B C

E

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 $1 and $2, not $3.
  • C. A C: Incorrect because $2 is B, not C. The function does not skip $2.
  • D. B C: Incorrect because $1 is A, not B.
  • E. C B A: Incorrect because the order is reversed and includes C which is not referenced.