nerdexam
LPI

010-150 · Question #61

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

The correct answer is C. "$2". In shell scripting, positional parameters are accessed with $1, $2, etc., and quoting with double quotes preserves the value while allowing variable expansion.

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"$1"
  • E'$2'

How the community answered

(24 responses)
  • B
    4% (1)
  • C
    79% (19)
  • D
    13% (3)
  • E
    4% (1)

Why each option

In shell scripting, positional parameters are accessed with `$1`, `$2`, etc., and quoting with double quotes preserves the value while allowing variable expansion.

A"$ARG2"

`$ARG2` is not a standard shell positional parameter; it would only work if explicitly defined elsewhere in the script.

B$1

`$1` accesses the first command line argument, not the second.

C"$2"Correct

The variable `$2` refers to the second positional argument passed to a script. Enclosing it in double quotes (`"$2"`) is best practice because it prevents word splitting and glob expansion on the value while still expanding the variable correctly.

D"$1"

`"$1"` also accesses the first argument, not the second.

E'$2'

Single quotes (`'$2'`) prevent variable expansion entirely, so the literal string `$2` is output rather than the argument value.

Concept tested: Shell positional parameters and argument access

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

Topics

#positional parameters#shell scripting#command line arguments#script syntax

Community Discussion

No community discussion yet for this question.

Full 010-150 Practice