bash – 回调shell转义参数

有没有一个命令,不只是回想它的参数,而且还需要转义(例如,如果参数包含空格或特殊字符)?

我需要它在一些shell魔术,而不是在一个脚本中执行命令我回应命令.该输出被管道连接到一个Python脚本,最终以更有效的方式执行命令(它加载了实际目标python脚本的main()方法,并使用给定的参数执行它,并且通过计算数据的附加参数被缓存在main())的运行.

而不是我当然可以将所有的shell魔术移植到python,我不需要管道任何东西.

使用bash,printf内置函数有一个额外的格式说明符%q,以友好的方式打印相应的参数:

In addition to the standard printf(1) formats,%b causes printf to expand backslash escape sequences in the corresponding argument (except that \c terminates output,backslashes in \',\",and \? are not removed,and octal escapes beginning with \0 may contain up to four digits),and %q causes printf to output the corresponding argument in a format that can be reused as shell input.

所以你可以这样做:

printf %q "$VARIABLE"
printf %q "$(my_command)"

以一种可以再次输入的格式(即空格转义)的方式获取变量或命令输出的内容.例如:

$printf "%q\n" "foo bar"
foo\ bar

(我添加了一个换行符,所以它将是一个漂亮的交互式shell.)

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...