Answers for "$@ instead of $* in bash functions"

0

$@ instead of $* in bash functions

#!/bin/bash

# $* is a single string whereas $@ is an array.

# Using "$*"
for a in "$*"; do
    echo $a;
done

# Result:
# one two three four

#-------------------------

# Using $*
for a in $*; do
    echo $a;
done

# Result:
# one 
# two 
# three 
# four

#-------------------------

# Using "$@"
for a in "$@"; do
    echo $a;
done

# Result:
# one 
# two 
# three four

#-------------------------

# Using $@
for a in $@; do
    echo $a;
done    

# Result:
# one 
# two 
# three 
# four
Posted by: Guest on April-15-2021

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language