Skip to content

Instantly share code, notes, and snippets.

@weynhamz
Created December 17, 2012 14:09
Show Gist options
  • Save weynhamz/4318541 to your computer and use it in GitHub Desktop.
Save weynhamz/4318541 to your computer and use it in GitHub Desktop.
Simple code that illustrates the difference between `$*` and `$@` in shell scripting.
#!/bin/bash
foo() {
printf "'%s'" $*
}
bar() {
printf "'%s'" $@
}
foofoo() {
printf "'%s'" "$*"
}
barbar() {
printf "'%s'" "$@"
}
foo 1 2 "3 4" # '1''2''3''4'
bar 1 2 "3 4" # '1''2''3''4'
foofoo 1 2 "3 4" # '1 2 3 4'
barbar 1 2 "3 4" # '1''2''3 4'
# Generally, we should use `"$@"` in case the parameter contains
# `IFS` character, `$*` or `$@` is safe if the the parameter does
# not contain `IFS` character, nerver use `"$*"`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment