data.in
:
a b c 'd e'
script.sh
:
while read -a arr; do
echo "${#arr[@]}"
for i in "${arr[@]}"; do
echo "$i"
done
done
Command:
cat data.in | bash script.sh
Output:
5
a
b
c
'd
e'
Question:
How can I get 'd e'
as a single element in the array?
Update. This is the best I've done so far:
while read line; do
arr=()
while read word; do
arr+=("$word")
done < <(echo "$line" | xargs -n 1)
echo "${#arr[@]}"
for i in "${arr[@]}"; do
echo "$i"
done
done
Output:
4
a
b
c
d e
However, the following data.in
:
"a\"b" c
will fail it (and any other script I have found so far, even in the dup question):
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
But this input is legal because you can type in command line:
echo "a\"b" c
And it runs well. So this is a mismatch in behavior not illegal input.