What is $@ in Bash?

BashArguments

Bash Problem Overview


I reckon that the handle $@ in a shell script is an array of all arguments given to the script. Is this true?

I ask because I normally use search engines to gather information, but I can't google for $@ and I have grown too accustomed to easily getting served everything.

Bash Solutions


Solution 1 - Bash

Yes. Please see the man page of Bash (the first thing you go to) under Special Parameters:

> Special Parameters > > The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed. > > * Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators. > > @ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

Solution 2 - Bash

Just from reading that I would have never understood that "$@" expands into a list of separate parameters. Whereas, "$*" is one parameter consisting of all the parameters added together.

If it still makes no sense, do this.

Bash special parameters explained with four example shell scripts

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvecvanView Question on Stackoverflow
Solution 1 - Bashghostdog74View Answer on Stackoverflow
Solution 2 - BashThomas ChilinskiView Answer on Stackoverflow