Created
December 17, 2012 14:09
-
-
Save weynhamz/4318541 to your computer and use it in GitHub Desktop.
Simple code that illustrates the difference between `$*` and `$@` in shell scripting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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