304

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.

Burhan
  • 600
  • 4
  • 24
vecvan
  • 3,049
  • 2
  • 14
  • 3
  • 13
    [You can search for it now](http://symbolhound.com/?q=bash+%24%40). – l0b0 Apr 03 '12 at 14:39
  • 14
    You should pipe things like 'man bash' into grep with the -C argument set to something reasonable, it's a really useful skill – djdanlib Apr 03 '12 at 14:41
  • 6
    The question linked (that this question is a duplicate of) was asked 2 years after this question... Shouldn't that question be a duplicate of this question and not the other way around? – mgarey Aug 24 '17 at 16:01
  • 9
    Google now searches for symbols, and your question is result #1! www.google.com/search?q=%24%40 – Chro Apr 25 '18 at 15:06
  • 1
    @mgarey https://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha – user202729 Jun 14 '18 at 07:46
  • 1
    @tripleee, you marked 2010 question as dup of 2012. And the accepted answer here IMHO is better that for dup one. Please reverse your marking. – Alexei Martianov Oct 24 '19 at 07:02
  • 1
    @Alexei The age of the question is a secondary concern, and the quality of the answers is usually the primary deciding factor. I don't remember my thinking exactly, but the other question has a broader selection of answers with more upvotes, so more nuance. But I'll be happy to discuss further; please visit the [bash chatroom](https://chat.stackoverflow.com/rooms/98569/bin-bash) or maybe post a question on meta and ping me here. – tripleee Oct 24 '19 at 07:50
  • ```~ man bash | grep -B 5 -A 5 -F '$@'``` – Dane_duPlessis Nov 15 '21 at 13:19

2 Answers2

235

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).

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
ghostdog74
  • 307,646
  • 55
  • 250
  • 337
  • 116
    oh boy, good that it has merely 5000 lines ;) – vecvan Oct 10 '10 at 02:27
  • 28
    Like djanlib said: use something like `man bash | grep -C2 '$@'` – cmbuckley Apr 03 '12 at 15:01
  • 191
    @vecvan number of questions before reading man bash: 1 ... number of questions after reading man bash: 12,031 – FloatingRock Oct 29 '14 at 16:05
  • 90
    Docs aren't always the best place to get an explanation of something. Thinking the writer can articulate the functionality of something in an easy to understand answer is a huge assumption. That's why we have this forum. – swade Apr 19 '18 at 15:26
  • 1
    like cmbuckley said, to learn from `man bash` you must, first, understand `man bash` – wamster Apr 22 '21 at 02:30
109

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Thomas Chilinski
  • 1,091
  • 1
  • 7
  • 2