Skip to the workspace
Pebellslearn by building
Arguments and String Manipulation  ›  Lesson

"$@", $* and shift

$@ 14 min"$@" expands positional arguments as separate words. "$*" joins them using the first character of IFS, and shift removes leading positional arguments.

shell bash / "$@", $* and shift

Explore each scene at your own pace.

01 · The rule

The rule

"$@", $* and shift

"$@" expands positional arguments as separate words. "$*" joins them using the first character of IFS, and shift removes leading positional arguments.

Forward arguments without merging words or losing empty strings.

backup.sh should accept any number of folders: ./backup.sh ~/notes "My Photos" ~/code. You cannot write $1 $2 $3 when you do not know how many will come. Bash has two ways to say "all the arguments", and only one of them is safe.

The rule

"$@" keeps each argument whole; shift drops the first.

For the call ./backup.sh ~/notes "My Photos":

Written as Becomes
"$@" two words: /home/asha/notes and My Photos
"$*" one word: /home/asha/notes My Photos
$@ or $* unquoted three words: /home/asha/notes, My, Photos

"$@" is exactly what the caller typed, argument for argument. It is the right choice almost every time you pass arguments on to another command:

tar -czf "$archive" "$@"

"$*" is for when you want one string, such as a log message: echo "Arguments: $*".

shift throws away $1 and moves every other argument down one place. $# drops by one. shift 2 drops two. This is how a script peels off leading options before handling the rest:

label="$1"          # save it first
shift               # now "$@" is only the folders
tar -czf "$dest/$label.tar.gz" "$@"
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.