nerdexam
LPI

010-160 · Question #59

Which of the following statements may be used to access the second command line argument to a script?

The correct answer is C. "$2". The shell assigns command-line arguments to positional parameters $1, $2, $3, etc., so $2 holds the second argument.

The Power of the Command Line

Question

Which of the following statements may be used to access the second command line argument to a script?

Options

  • A"$ARG2"
  • B"$1"
  • C"$2"
  • D"$S2"
  • E'$2'

How the community answered

(28 responses)
  • C
    89% (25)
  • D
    4% (1)
  • E
    7% (2)

Why each option

The shell assigns command-line arguments to positional parameters $1, $2, $3, etc., so $2 holds the second argument.

A"$ARG2"

$ARG2 is not a special shell variable; it would only hold a value if the script explicitly assigned one to it.

B"$1"

$1 holds the first positional argument, not the second.

C"$2"Correct

$2 is the standard POSIX positional parameter for the second command-line argument. The shell automatically populates $1 through $N with successive arguments, making $2 the correct and idiomatic way to access the second one inside a script.

D"$S2"

$S2 is not a recognized positional parameter or special shell variable; it would evaluate to an empty string unless explicitly set.

E'$2'

Single quotes suppress all parameter expansion in bash, so '$2' produces the literal four-character string $2 rather than the value of the second argument.

Concept tested: Shell script positional parameter access

Source: https://www.gnu.org/software/bash/manual/bash.html#Positional-Parameters

Topics

#positional parameters#command arguments#bash variables#shell scripting

Community Discussion

No community discussion yet for this question.

Full 010-160 Practice