Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is $@ in Bash? [duplicate]

Tags:

bash

arguments

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.

like image 725
vecvan Avatar asked Oct 10 '10 01:10

vecvan


People also ask

What is $@ In a bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is $@ in Linux?

$@ is basically use for refers all the command-line arguments of shell-script. $1 , $2 , $3 refer to the first command-line argument, the second command-line argument, third argument. Follow this answer to receive notifications.

What does $@ mean in Unix?

$@ refers to all of a shell script's command-line parameters. $1 , $2 , etc., refer to the first command-line parameter, the second command-line parameter, etc. Place variables in quotes if the values might have spaces in them.

Does $@ include $0?

Difference between “$0” and “$@” in Unix shell scripts.. They are entirely different. $0 is the name of the script; "$@" expands to the command-line arguments.


2 Answers

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

like image 135
ghostdog74 Avatar answered Sep 20 '22 09:09

ghostdog74


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

like image 20
Thomas Chilinski Avatar answered Sep 22 '22 09:09

Thomas Chilinski