Bash * and @ Relative to Array Elements: Difference between revisions
Jump to navigation
Jump to search
(→...*) |
|||
Line 13: | Line 13: | ||
=<tt>...*</tt>= | =<tt>...*</tt>= | ||
Each element in the array is represented as an individual string. The arguments that contain spaces are tokenized, and the spaces are discarded. In other words, the arguments that contain spaces are not joined together, but handled as different tokens. | |||
In case of command line arguments handled as such: | |||
<syntaxhighlight lang='bash'> | |||
for i in $*; do | |||
echo ${i} | |||
done | |||
</syntaxhighlight> | |||
calling the program with: | |||
<syntaxhighlight lang='bash'> | |||
./test.sh "A B" "C D" | |||
</syntaxhighlight> | |||
outputs: | |||
<syntaxhighlight lang='bash'> | |||
A | |||
B | |||
C | |||
D | |||
</syntaxhighlight> | |||
=<tt>...@</tt>= | =<tt>...@</tt>= | ||
=<tt>"...@"</tt>= | =<tt>"...@"</tt>= |
Revision as of 18:52, 1 April 2024
Internal
- $@
- $*
- Get All Indexed Array Elements
- Get All Associative Array Keys
- Get All Associative Array Values
Overview
*
and @
are bash syntax elements that tokenize arrays. They are used to process command line argument ($*
, $@
and "$@"
), values for indexed arrays, keys and values for associative arrays, etc.
The difference between these two syntax elements becomes apparent when the array elements contain spaces:
...*
Each element in the array is represented as an individual string. The arguments that contain spaces are tokenized, and the spaces are discarded. In other words, the arguments that contain spaces are not joined together, but handled as different tokens.
In case of command line arguments handled as such:
for i in $*; do
echo ${i}
done
calling the program with:
./test.sh "A B" "C D"
outputs:
A
B
C
D