347

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
  • 668
  • 5
  • 27
vecvan
  • 3,479
  • 2
  • 15
  • 3
  • 14
    [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
  • 8
    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
  • 10
    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. – Alex Martian 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

269

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,738
  • 21
  • 105
  • 131
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 143
    oh boy, good that it has merely 5000 lines ;) – vecvan Oct 10 '10 at 02:27
  • 35
    Like djanlib said: use something like `man bash | grep -C2 '$@'` – cmbuckley Apr 03 '12 at 15:01
  • 221
    @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
  • 113
    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
  • 2
    like cmbuckley said, to learn from `man bash` you must, first, understand `man bash` – wamster Apr 22 '21 at 02:30
  • Neither this answer or @ThomasChilinski's explained things such as: when do you want or not want to use quotes with `$@`, for example? The official bash docs don't even explain that. – trusktr Aug 15 '22 at 19:29
  • @cmbuckley to search in a generic manpage, you have the slash command. Open the manpage you are interested in, then type the slash, then type what you want to search. To others there are people who read manpages and then know how things work. I am one of those, and we are accepting new members in the team. For example, the info about the slash command is inside a manpage. I leave you as an exercise to find the manpage itself. – vaeVictis Nov 01 '22 at 14:47
  • 1
    @wamster to understand man bash you have to read it from top to the bottom, a couple of time. I printed it and it helped. It is perfectly clear and it explains almost everything, and even more. – vaeVictis Nov 01 '22 at 14:48
  • @vaeVictis I'll put that on my bucket list :) – wamster Nov 25 '22 at 00:06
  • 1
    @wamster glad to see I wrote something interesting. Bash man is an overkill sometimes. Always worth it though. Enjoy! – vaeVictis Nov 25 '22 at 07:18
  • @trusktr it is clearly explained in the Bash man when to quote $@. Just look for special parameters. – vaeVictis Aug 01 '23 at 07:23
136

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,738
  • 21
  • 105
  • 131
Thomas Chilinski
  • 1,361
  • 1
  • 8
  • 2