What Does $@ Mean in a Shell Script

What does $@ mean in a shell script?

$@ is nearly the same as $*, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).

The difference between the two syntaxes shows up when you have an argument with spaces in it (e.g.) and put $@ in double quotes:

wrappedProgram "$@"
# ^^^ this is correct and will hand over all arguments in the way
# we received them, i. e. as several arguments, each of them
# containing all the spaces and other uglinesses they have.
wrappedProgram "$*"
# ^^^ this will hand over exactly one argument, containing all
# original arguments, separated by single spaces.
wrappedProgram $*
# ^^^ this will join all arguments by single spaces as well and
# will then split the string as the shell does on the command
# line, thus it will split an argument containing spaces into
# several arguments.

Example: Calling

wrapper "one two    three" four five "six seven"

will result in:

"$@": wrappedProgram "one two    three" four five "six seven"
"$*": wrappedProgram "one two three four five six seven"
^^^^ These spaces are part of the first
argument and are not changed.
$*: wrappedProgram one two three four five six seven

what does $* mean in a shell script

It means all the arguments passed to the script or function, split by word.

It is usually wrong and should be replaced by "$@", which separates the arguments properly.

what does '$?' mean in a shell script?

$?

returns the status of the last finished command.
Status 0 tells you that everything finished ok.

In addition the $ sign is a special symbol - and in that case $val extract the value that is hold by the variable val

What does ${1+$@} mean in a unix shell script?

I found this explanation from unix haters handbook, page 152 of text (page 190 of the pdf). http://web.mit.edu/~simsong/www/ugh.pdf

It’s the way to exactly reproduce the command line arguments in the
/bin/sh family of shells shell script.

It says, “If there is at least one argument ( ${1+ ), then substitute in
all the arguments ( “$@” ) preserving all the spaces, etc. within each
argument.

If we used only “$@” then that would substitute to “” (a null argument)
if there were no invocation arguments, but we want no arguments reproduced in
that case, not “”.

Why not “$*” etc.? From a sh(1) man page:

Inside a pair of double quote marks (“”), parameter and
command substitution occurs and the shell quotes the results to
avoid blank interpretation and file name generation. If $* is
within a pair of double quotes, the positional parameters are
substituted and quoted, separated by quoted spaces (“$1
$2 …”); however, if $@ is within a pair of double quotes, the
positional parameters are substituted and quoted, separated by
unquoted spaces (“$1” “$2” …).

I think ${1+“$@”} is portable all the way back to “Version 7 Unix.”

Wow! All the way back to Version 7.

What is the meaning of -n, -z, -x, -L, -d, etc... in Shell Script?

IMHO best way is you could simply do man test for all these details. It is very well explained there. As follows is the text from man page. For BASH conditional expressions look for link https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html too once.

   -b FILE
FILE exists and is block special

-c FILE
FILE exists and is character special

-d FILE
FILE exists and is a directory

-e FILE
FILE exists

-f FILE
FILE exists and is a regular file

-g FILE
FILE exists and is set-group-ID

-G FILE
FILE exists and is owned by the effective group ID

-h FILE
FILE exists and is a symbolic link (same as -L)

-k FILE
FILE exists and has its sticky bit set

-L FILE
FILE exists and is a symbolic link (same as -h)

-O FILE
FILE exists and is owned by the effective user ID

-p FILE
FILE exists and is a named pipe

-r FILE
FILE exists and read permission is granted

-s FILE
FILE exists and has a size greater than zero

-S FILE
FILE exists and is a socket

-t FD file descriptor FD is opened on a terminal

-u FILE
FILE exists and its set-user-ID bit is set

-w FILE
FILE exists and write permission is granted

-x FILE
FILE exists and execute (or search) permission is granted

For expressions in man test it is given:

   ( EXPRESSION )
EXPRESSION is true

! EXPRESSION
EXPRESSION is false

EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true

EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true

-n STRING
the length of STRING is nonzero

STRING equivalent to -n STRING

-z STRING
the length of STRING is zero

STRING1 = STRING2
the strings are equal

STRING1 != STRING2
the strings are not equal

INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2

For conditional expressions info go for man bash it gives info as follows too.

CONDITIONAL EXPRESSIONS
Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and
perform string and
arithmetic comparisons. Expressions are formed from the following unary or binary primaries. If any file argument to one of
the primaries is
of the form /dev/fd/n, then file descriptor n is checked. If the file argument to one of the primaries is one of /dev/stdin,
/dev/stdout, or
/dev/stderr, file descriptor 0, 1, or 2, respectively, is checked.

   Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather 

than the link
itself.

   When used with [[, The < and > operators sort lexicographically using the current locale.

Meaning of @ in bash scripts

The GNU man bash page would be a good place to start

@

($@) 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).

A small example to demonstrate the same,

function foo() {
printf "%s\n" "$@"
}

foo 1 2 3

would print

1
2
3

Does $! mean something in shell scripting

In addition to other answer, this echo

echo $!

Will print blank if you haven't yet run any process in background in current shell. If you now run:

date &
echo $!

Then it will print something like (i.e. process id of last executed background process):

47833


Related Topics



Leave a reply



Submit