简体   繁体   中英

Assigning jq output to bash array when json value contains spaces

When I do this:

arr=($(echo '{"crew":[{"name":"kirk"},{"name":"bones"},{"name":"mr spock"}]}' | jq -r '.crew[].name | @sh'))

I get:

echo "${arr[2]}"
mr
echo "${arr[3]}"
spock

However when I do this:

arr=("kirk" "bones" "mr spock")

I get this:

echo "${arr[2]}"
mr spock

Why, in the first example, is bash ignoring the quotes that each jq value is wrapped in when it creates the array?

When I do this:

arr=($(echo '{"crew":[{"name":"kirk"},{"name":"bones"},{"name":"mr spock"}]}' | jq -r '.crew[].name | @sh'))

I get:

echo "${arr[2]}"
mr
echo "${arr[3]}"
spock

However when I do this:

arr=("kirk" "bones" "mr spock")

I get this:

echo "${arr[2]}"
mr spock

Why, in the first example, is bash ignoring the quotes that each jq value is wrapped in when it creates the array?

When I do this:

arr=($(echo '{"crew":[{"name":"kirk"},{"name":"bones"},{"name":"mr spock"}]}' | jq -r '.crew[].name | @sh'))

I get:

echo "${arr[2]}"
mr
echo "${arr[3]}"
spock

However when I do this:

arr=("kirk" "bones" "mr spock")

I get this:

echo "${arr[2]}"
mr spock

Why, in the first example, is bash ignoring the quotes that each jq value is wrapped in when it creates the array?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM