Created
June 5, 2015 22:23
-
-
Save trepmal/25faf3c274d8ff7b4173 to your computer and use it in GitHub Desktop.
Fun with positional args and quoting in bash
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 | |
# 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