Skip to main content
Word & grammar fixes
Source Link
Jeff Schaller
  • 67.7k
  • 35
  • 118
  • 255

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
  • 155.1k
  • 39
  • 335
  • 412

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
  • 155.1k
  • 39
  • 335
  • 412

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 "$@".