Last active
February 20, 2024 14:36
-
-
Save kiprasmel/39eeb1a97dfdbd1b0a957f389f1f0a13 to your computer and use it in GitHub Desktop.
bash argv forwarding: $* vs $@ vs "$*" vs "$@" -- use "$@"
This file contains hidden or 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
#!/usr/bin/env bash | |
fn() { | |
base="$1" | |
shift | |
printf "$base with \$*\n" | |
for i in $* ; do printf " $i\n"; done; | |
printf "$base with \$@\n" | |
for i in $@ ; do printf " $i\n"; done; | |
printf "$base with \"\$*\"\n" | |
for i in "$*"; do printf " $i\n"; done; | |
printf "$base with \"\$@\"\n" | |
for i in "$@"; do printf " $i\n"; done; | |
printf "\n" | |
} | |
fn '$*' $* | |
fn '$@' $@ | |
fn '\"$*\"' "$*" | |
fn '\"$@\"' "$@" | |
# singular | |
#for i in $* ; do echo $i; done; echo "" | |
#for i in $@ ; do echo $i; done; echo "" | |
#for i in "$*"; do echo $i; done; echo "" | |
#for i in "$@"; do echo $i; done; echo "" |
This file contains hidden or 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
$ ./bash-argv-forwarding a "b c" | |
$* with $* | |
a | |
b | |
c | |
$* with $@ | |
a | |
b | |
c | |
$* with "$*" | |
a b c | |
$* with "$@" | |
a | |
b | |
c | |
$@ with $* | |
a | |
b | |
c | |
$@ with $@ | |
a | |
b | |
c | |
$@ with "$*" | |
a b c | |
$@ with "$@" | |
a | |
b | |
c | |
"$*" with $* | |
a | |
b | |
c | |
"$*" with $@ | |
a | |
b | |
c | |
"$*" with "$*" | |
a b c | |
"$*" with "$@" | |
a b c | |
"$@" with $* | |
a | |
b | |
c | |
"$@" with $@ | |
a | |
b | |
c | |
"$@" with "$*" | |
a b c | |
"$@" with "$@" | |
a | |
b c | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment