为其他命令转义嵌入字符串的最兼容方式

Most compatible way of escaping embedded strings for other commands

我目前正在使用以下解决方案。

对于文字 'string'

variable="'"$(echo -E "$variable" | perl -pe "s/'/'\"'\"'/g;")"'"

对于文字 "string"

variable='"'$(echo -E "$variable" | perl -pe 's/(?=[\"])/\/g;')'"'

你知道更好的吗?

I know we can execute with temporary environment variables.

NAME=VALUE command

However, some programs do not provide any ways for accessing them.

AppleScript 示例


编辑

我发现 AppleScript 可以使用 on run argv ~ end run 语法处理参数。

引号是 AppleScript 语法的一部分,因此应与实际数据分开。

osascript -e "log \"$variable\""

osascript -e "log '$variable'"