Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created June 5, 2015 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/25faf3c274d8ff7b4173 to your computer and use it in GitHub Desktop.
Save trepmal/25faf3c274d8ff7b4173 to your computer and use it in GitHub Desktop.
Fun with positional args and quoting in bash
#!/bin/bash
# argtest.sh
#
# bash positional args and quoting
greenon='\033[0;92m';
coloroff='\033[0m'
###### #######
## ## ## ## ##
## ## ## ### ##
###### ## ### ##
## ## ## #####
## ## ## ##
###### #######
echo -e "$greenon\$@, Both quoted$coloroff"
for word in "$@"; do
echo "$word"
done;
#$ bash argtest.sh one "two three" four
#> one
#> two three
#> four
echo -e "$greenon\$@, Both unquoted$coloroff"
for word in $@; do
echo $word
done;
#$ bash argtest.sh one "two three" four
#> one
#> two
#> three
#> four
echo -e "$greenon\$@, \$@ unquoted$coloroff"
for word in $@; do
echo "$word"
done;
#$ bash argtest.sh one "two three" four
#> one
#> two
#> three
#> four
echo -e "$greenon\$@, word unquoted$coloroff"
for word in "$@"; do
echo $word
done;
#$ bash argtest.sh one "two three" four
#> one
#> two three
#> four
###### #
## ## ## #####
## ## ###
###### # #
## ##
## ## ##
######
echo -e "$greenon\$*, Both quoted$coloroff"
for word in "$*"; do
echo "$word"
done;
#$ bash argtest.sh one "two three" four
#> one two three four
echo -e "$greenon\$*, Both unquoted$coloroff"
for word in $*; do
echo $word
done;
#$ bash argtest.sh one "two three" four
#> one
#> two
#> three
#> four
echo -e "$greenon\$*, \$* unquoted$coloroff"
for word in $*; do
echo "$word"
done;
#$ bash argtest.sh one "two three" four
#> one
#> two
#> three
#> four
echo -e "$greenon\$*, word unquoted$coloroff"
for word in "$*"; do
echo $word
done;
#$ bash argtest.sh one "two three" four
#> one two three four
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment