Ubuntu – What does ${_[0]} mean in bash

bashcommand line

When I type this in bash command line

$ x=hi; printf '%s ' "$x" "${x[0]}"; echo "${_[0]}"

I have this output:

hi hi hi
  • Why "${_[0]}" turns out to be "hi" in the output?
  • Why could we use the "x[0]" syntax, given that "x"is just a string instead of an array?

Best Answer

The _ parameter has several meanings depending on context, but it is never an array. Likewise, in your example, x is not an array. The reason you're able to treat it as if it is an array is that Bash allows non-array variables to be treated as if they were one-element arrays. Bash likewise allows array variables to be treated as if they were non-arrays, giving the first element.

As man bash says:

Referencing an array variable without a subscript is equivalent to referencing the array with a subscript of 0. Any reference to a variable using a valid subscript is legal, and bash will create an array if necessary.

So "${_[0]}" behaves the same as "${_}" or "$_", because _ is not an array. Likewise, "${x[0]}" behaves the same as "${x}" or "$x", because x is not an array.


As for why _ holds the value hi: In the usage you've shown, performing parameter expansion on the special _ parameter yields the last argument of the most recent (synchronously executed) command.

As man bash says of _:

At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous simple command executed in the foreground, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked.

(emphasis mine)

In this case, the most recently executed command was:

printf '%s ' "$x" "${x[0]}"

The arguments passed to printf were:

  • %s , on which only quote removal was performed.
  • hi, on which parameter expansion was performed, followed by quote removal.
  • hi, on which a more complex form of parameter expansion yielding the same result was performed, followed by quote removal.