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

后端 未结 3 2050
一生所求
一生所求 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"
    ▶
    
    0 讨论(0)
  • 2020-11-29 21:30

    It causes escape sequences to be interpreted.

    $ echo $'Name\tAge\nBob\t24\nMary\t36'
    Name    Age
    Bob     24
    Mary    36
    

    (And SO handles tabs in a goofy manner, so try this one at home)

    0 讨论(0)
  • 2020-11-29 21:42

    Using $ as a prefix tells BASH to try to find a variable with that name. $' is a special syntax (fully explained here) which enables ANSI-C string processing. In this case, the single tick isn't "take value verbatim until the next single tick". It should be quite safe to use. The drawbacks are it's BASH only and quite uncommon, so many people will wonder what it means.

    The better way is to use single quotes. If you need a single quote in a string, you need to replace it with '\''. This ends the previous single quoted string, adds a single quote to it (\') and then starts a new single quoted string. This syntax works with any descendant of the Bourne shell, it's pretty easy to understand and most people quickly recognize the pattern.

    The alternative is to replase each single tick with '"'"' which translates to "terminate current single quoted string, append double quoted string which contains just a single tick, restart single quoted string". This avoid the escape character and looks nicely symmetric. It also works the other way around if you need a double quote in a double quoted string: "'"'".

    0 讨论(0)
提交回复
热议问题