将一个字符串转换使用bash数组,表彰为分组报价 [英] Convert a string into an array with bash, honoring quotes for grouping

查看:121
本文介绍了将一个字符串转换使用bash数组,表彰为分组报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串为:

Str='This string has "a substring"'

该字符串逗号,所以如果我打印字符串我看到:

The string has commas so if I print the string i see:

echo "${Str}"

This string has "a substring". 

如果我输入下面的命令:

If I input the command:

$ Tmp=( ${Str} )
$ echo "${Tmp[3]}"
"a
$ echo "${Tmp[4]}"
Substring"

我还想打印: A子串
任何建议?
我可以改变逗号,但它是必不可少的,它会从海峡被打印到TMP

Id like to print: a Substring Any advice? I can change the commas but it is essential that it will be printed from Str to Tmp

推荐答案

给一个尝试这样的:

 Str='this string has "a substring"'
 eval Tmp=( "${Str}" )

 printf "%s\n" "${Str}"
 this string has "a substring"

 printf "%s\n" "${Tmp[3]}"
 a substring

 set | grep "^Tmp"
 Tmp=([0]="this" [1]="string" [2]="has" [3]="a substring")

我不得不提醒你关于评估,看到@charlesduffy评论:用它只有当海峡是早期用自己的code线产生的。

I have to warn you regarding eval, see the comment from @charlesduffy: use it only if Str is generated earlier with your own lines of code.

这篇关于将一个字符串转换使用bash数组,表彰为分组报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆