Mohammedz.com

For Linux and Shell scripting.

Difference between $* and $@

Leave a comment


Run the script below and feel the difference 😉

shell]$ cat input
#! /bin/bash

echo “$# inputs”
echo “”
echo ‘”$@” output’
for arg in “$@”; do
echo $arg
done
echo “”
echo ‘$@ output’
for arg in $@; do
echo $arg
done
echo “”
echo ‘”$*” output’
for arg in “$*”; do
echo $arg
done
echo “”
echo ‘$* output’
for arg in $*; do
echo $arg
done
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
shell]$ ./input a b ‘c d’
3 inputs

“$@” output
a
b
c d

$@ output
a
b
c
d

“$*” output
a b c d

$* output
a
b
c
d
shell]$

Leave a comment