Monday, January 19, 2015

One Simple Example To Illustrate The Difference Between $@ and $* In Shell

The following code is well self-explained:
bash test.sh 123 "456 7" 8

#!/bin/bash

echo "--\"\$*\"--"
for args in "$*"
do
    echo "#"$args"#"
done

echo "--\$*--"
for args in $*
do
    echo "#"$args"#"
done

echo "--\"\$@\"--"
for args in "$@"
do
    echo "#"$args"#"
done

echo "--\$@--"
for args in $@
do
    echo "#"$args"#"
done

Output:
--"$*"--
#123 456 7 8#
--$*--
#123#
#456#
#7#
#8#
--"$@"--
#123#
#456 7#
#8#
--$@--
#123#
#456#
#7#
#8#


Reference:
1. Difference between $@ and $* - CSDN



© 2014-2017 jason4zhu.blogspot.com All Rights Reserved 
If transfering, please annotate the origin: Jason4Zhu

No comments:

Post a Comment