开发者

String concatenation for a bash script command

开发者 https://www.devze.com 2023-01-07 01:51 出处:网络
I\'m trying to write an alias in my ~/.bashrc of this form: alias a=\"foo; osascript -e \'tell application \"Terminal\" to do script \"bar\"; baz\"

I'm trying to write an alias in my ~/.bashrc of this form:

alias a="foo; osascript -e 'tell application "Terminal" to do script "bar"; baz"

(where bar launches in a new Terminal window, as per this question) but this string doesn't look like it will parse. I开发者_C百科 tried string interpolation (${str}), but the problem seems to be unsolvable that way.


alias a="foo; osascript -e 'tell application \"Terminal\" to do script \"bar\"'; baz"


Surprisingly, the string appears to work fine as written. As long as it's on one line, Bash doesn't terminate the double-quoted string the way you'd expect.

Update: As Ignacio points out, the string as written doesn't preserve the double quotes within it. To wit:

# Good:
a="foo; osascript -e 'tell application \"Terminal\" to do script \"bar\"'; baz"
echo $a
> foo; osascript -e 'tell application "Terminal" to do script "bar"'; baz

# Not so good:
b="foo; osascript -e 'tell application "Terminal" to do script "bar"'; baz"
echo $b
> foo; osascript -e 'tell application Terminal to do script bar'; baz
0

精彩评论

暂无评论...
验证码 换一张
取 消