Skip to main content
Word & grammar fixes
Source Link
Jeff Schaller
  • 68.9k
  • 35
  • 122
  • 268

Here is a simple script to demonstrates the differentdifference between $* and $@:

#!/bin/bash test_param() { echo "Receive $# parameters" echo Using '$*' echo for param in $*; do printf '==>%s<==\n' "$param" done; echo echo Using '"$*"' for param in "$*"; do printf '==>%s<==\n' "$param" done; echo echo Using '$@' for param in $@; do printf '==>%s<==\n' "$param" done; echo echo Using '"$@"'; for param in "$@"; do printf '==>%s<==\n' "$param" done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh Receive 4 parameters Using $* ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, thethere is no differentdifference when using $* or $@. It only make sense when you use them with double quotes "$*" and "$@".

Here is a simple script to demonstrates the different between $* and $@:

#!/bin/bash test_param() { echo "Receive $# parameters" echo Using '$*' echo for param in $*; do printf '==>%s<==\n' "$param" done; echo echo Using '"$*"' for param in "$*"; do printf '==>%s<==\n' "$param" done; echo echo Using '$@' for param in $@; do printf '==>%s<==\n' "$param" done; echo echo Using '"$@"'; for param in "$@"; do printf '==>%s<==\n' "$param" done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh Receive 4 parameters Using $* ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, the no different when using $* or $@. It only make sense when you use with double quotes "$*" and "$@".

Here is a simple script to demonstrates the difference between $* and $@:

#!/bin/bash test_param() { echo "Receive $# parameters" echo Using '$*' echo for param in $*; do printf '==>%s<==\n' "$param" done; echo echo Using '"$*"' for param in "$*"; do printf '==>%s<==\n' "$param" done; echo echo Using '$@' for param in $@; do printf '==>%s<==\n' "$param" done; echo echo Using '"$@"'; for param in "$@"; do printf '==>%s<==\n' "$param" done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh Receive 4 parameters Using $* ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, there is no difference when using $* or $@. It only make sense when you use them with double quotes "$*" and "$@".

deleted 73 characters in body
Source Link
cuonglm
  • 158.3k
  • 41
  • 342
  • 421

Here is a simple script to demonstrates the different between $* and $@:

#!/bin/bash function test_param() {     echo "Receive $# parameters";parameters"   echo Using '$*';'$*'   echo   for param in $*;  do   printf echo'==>%s<==\n' "==>$param<==";"$param"   done;   echo   echo Using '"$*"';'"$*"'   for param in "$*";  do   printf echo'==>%s<==\n' "==>$param<==";"$param"   done;   echo   echo Using '$@';'$@'   for param in $@;  do   printf echo'==>%s<==\n' "==>$param<==";"$param"   done;   echo   echo Using '"$@"';   for param in "$@";  do   printf echo'==>%s<==\n' "==>$param<==";"$param"   done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh  Receive 4 parameters Using $*  ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, the no different when using $* or $@. It only make sense when you use with double quotes "$*" and "$@".

Here is a simple script to demonstrates the different between $* and $@:

#!/bin/bash function test_param() {     echo "Receive $# parameters";   echo Using '$*';   echo   for param in $*;  do   echo "==>$param<==";   done;   echo   echo Using '"$*"';   for param in "$*";  do   echo "==>$param<==";   done;   echo   echo Using '$@';   for param in $@;  do   echo "==>$param<==";   done;   echo   echo Using '"$@"';   for param in "$@";  do   echo "==>$param<==";   done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh  Receive 4 parameters Using $*  ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, the no different when using $* or $@. It only make sense when you use with double quotes "$*" and "$@".

Here is a simple script to demonstrates the different between $* and $@:

#!/bin/bash test_param() { echo "Receive $# parameters" echo Using '$*' echo for param in $*; do printf '==>%s<==\n' "$param" done; echo echo Using '"$*"' for param in "$*"; do printf '==>%s<==\n' "$param" done; echo echo Using '$@' for param in $@; do printf '==>%s<==\n' "$param" done; echo echo Using '"$@"'; for param in "$@"; do printf '==>%s<==\n' "$param" done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh Receive 4 parameters Using $* ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, the no different when using $* or $@. It only make sense when you use with double quotes "$*" and "$@".

Post Merged (destination) from unix.stackexchange.com/questions/94126/should-i-use-or
Source Link
cuonglm
  • 158.3k
  • 41
  • 342
  • 421

Here is a simple script to demonstrates the different between $* and $@:

#!/bin/bash function test_param() { echo "Receive $# parameters"; echo Using '$*'; echo for param in $*; do echo "==>$param<=="; done; echo echo Using '"$*"'; for param in "$*"; do echo "==>$param<=="; done; echo echo Using '$@'; for param in $@; do echo "==>$param<=="; done; echo echo Using '"$@"'; for param in "$@"; do echo "==>$param<=="; done } IFS="^${IFS}" test_param 1 2 3 "a b c" 

Output:

% cuonglm at ~ % bash test.sh Receive 4 parameters Using $* ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$*" ==>1^2^3^a b c<== Using $@ ==>1<== ==>2<== ==>3<== ==>a<== ==>b<== ==>c<== Using "$@" ==>1<== ==>2<== ==>3<== ==>a b c<== 

In array syntax, the no different when using $* or $@. It only make sense when you use with double quotes "$*" and "$@".