bash – 回调shell转义参数

前端之家收集整理的这篇文章主要介绍了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.)

原文链接:https://www.f2er.com/bash/386689.html

猜你在找的Bash相关文章