How does the leading dollar sign affect single quotes in Bash?

后端 未结 3 2070
一生所求
一生所求 2020-11-29 20:54

I need to pass a string to a program as its argument from the Bash CLI, e.g

program \"don\'t do this\"

The string may include any character

3条回答
  •  臣服心动
    2020-11-29 21:21

    You won't find a faster or more efficient way than:

    eval RESULT=\$\'$STRING\'
    

    For one, this is the kind of thing that eval is there for, and you avoid the crazy cost of forking subprocess all over the place, as the previous answers seem to suggest. Your example:

    $ foo='\u25b6'
    $ eval bar=\$\'$foo\'
    $ echo "$bar"
    ▶
    

提交回复
热议问题