Skip to content

Instantly share code, notes, and snippets.

@kiprasmel
Last active February 20, 2024 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiprasmel/39eeb1a97dfdbd1b0a957f389f1f0a13 to your computer and use it in GitHub Desktop.
Save kiprasmel/39eeb1a97dfdbd1b0a957f389f1f0a13 to your computer and use it in GitHub Desktop.
bash argv forwarding: $* vs $@ vs "$*" vs "$@" -- use "$@"
#!/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 ""
$ ./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