[英]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?为什么在第一个示例中,bash 在创建数组时忽略了每个 jq 值包含的引号?
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?为什么在第一个示例中,bash 在创建数组时忽略了每个 jq 值包含的引号?
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?为什么在第一个示例中,bash 在创建数组时忽略了每个 jq 值包含的引号?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.