Quantcast
Channel: What is the difference between $* and $@? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 12

Answer by Carlos Campderrós for What is the difference between $* and $@?

$
0
0

The difference is important when writing scripts that should use the positional parameters in the right way...

Imagine the following call:

$ myuseradd -m -c "Carlos Campderrós" ccampderros

Here there are just 4 parameters:

$1 => -m
$2 => -c
$3 => Carlos Campderrós
$4 => ccampderros

In my case, myuseradd is just a wrapper for useradd that accepts the same parameters, but adds a quota for the user:

#!/bin/bash -e

useradd "$@"
setquota -u "${!#}" 10000 11000 1000 1100

Notice the call to useradd "$@", with $@ quoted. This will respect the parameters and send them as-they-are to useradd. If you were to unquote $@ (or to use $* also unquoted), useradd would see 5 parameters, as the 3rd parameter which contained a space would be split in two:

$1 => -m
$2 => -c
$3 => Carlos
$4 => Campderrós
$5 => ccampderros

(and conversely, if you were to use "$*", useradd would only see one parameter: -m -c Carlos Campderrós ccampderros)

So, in short, if you need to work with parameters respecting multi-word parameters, use "$@".


Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>