This bash script
i=0
var='bonjour ? hello'
for v in $var
do ((i++))
echo -e "$i)$v"
done
will output:
1)bonjour
2)-
3)hello
not the expected:
1)bonjour
2)?
3)hello
Why ?
This bash script
i=0
var='bonjour ? hello'
for v in $var
do ((i++))
echo -e "$i)$v"
done
will output:
1)bonjour
2)-
3)hello
not the expected:
1)bonjour
2)?
3)hello
Why ?
This happens because you're expanding $var
unquoted, which replaces ?
with a list of single-character filenames in the current directory. You evidently ran this code in a directory containing a file named -
.
Lists of things should only ever be stored in arrays, not strings.
This lets you quote the expansion to suppress globbing and word-splitting, while still iterating over list elements:
#!/usr/bin/env bash
i=0
varArray=( 'bonjour' '?' 'hello' )
for v in "${varArray[@]}"; do
((i++))
echo "$i)$v"
done
If your input comes in as a string, you can use read -a
to convert it into an array without performing glob expansion:
#!/usr/bin/env bash
i=0
var='bonjour ? hello'
read -r -a varArray <<<"$var"
for v in "${varArray[@]}"; do
((i++))
echo "$i)$v"
done