Bash:“printf %q $str”在脚本中删除空格。(备择方案?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9045001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:26:59  来源:igfitidea点击:

Bash: "printf %q $str" deletes spaces when in scripts. (Alternatives?)

bashprintf

提问by Elena

printf %qshould quote a string. However, when executed into a script, it deletes the spaces.

printf %q应该引用一个字符串。但是,当在脚本中执行时,它会删除空格。

This command:

这个命令:

printf %q "hello world"

outputs:

输出:

hello\ world

which is correct.

哪个是正确的。

This script:

这个脚本:

#!/bin/bash

str="hello world"
printf %q $str

outputs:

输出:

helloworld

which is wrong.

这是错误的。

If such behavior is indeed expected, what alternative exists in a script for quoting a string containing any character in a way that it can be translated back to the original by a called program?

如果确实预料到了这种行为,那么脚本中存在什么替代方法来引用包含任何字符的字符串,以便可以通过被调用的程序将其翻译回原始字符串?

Thanks.

谢谢。

Software: GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

软件:GNU bash,版本 4.1.5(1)-release (i486-pc-linux-gnu)

EDITED: Solved, thanks.

编辑:已解决,谢谢。

回答by Susam Pal

You should use:

你应该使用:

printf %q "$str"

Example:

例子:

susam@nifty:~$ cat a.sh
#!/bin/bash

str="hello world"
printf %q "$str"
susam@nifty:~$ ./a.sh 
hello\ world

When you run printf %q $str, the shell expands it to:

运行时printf %q $str,shell 将其扩展为:

printf %q hello world

So, the strings helloand worldare supplied as two separate arguments to the printfcommand and it prints the two arguments side by side.

因此,字符串helloworld作为printf命令的两个单独参数提供,并并排打印这两个参数。

But when you run printf %q "$str", the shell expands it to:

但是当您运行时printf %q "$str",shell 将其扩展为:

printf %q "hello world"

In this case, the string hello worldis supplied as a single argument to the printfcommand. This is what you want.

在这种情况下,字符串hello world作为printf命令的单个参数提供。这就是你想要的。

Here is something you can experiment with to play with these concepts:

以下是您可以尝试使用这些概念的方法:

susam@nifty:~$ showargs() { echo "COUNT: $#"; printf "ARG: %s\n" "$@"; }
susam@nifty:~$ showargs hello world
COUNT: 2
ARG: hello
ARG: world
susam@nifty:~$ showargs "hello world"
COUNT: 1
ARG: hello world
susam@nifty:~$ showargs "hello world" "bye world"
COUNT: 2
ARG: hello world
ARG: bye world
susam@nifty:~$ str="hello world"
susam@nifty:~$ showargs $str
COUNT: 2
ARG: hello
ARG: world
susam@nifty:~$ showargs "$str"
COUNT: 1
ARG: hello world

回答by Zsolt Botykai

Try

尝试

printf %q "${str}"

in your script.

在你的脚本中。

回答by user3606895

This worked for me. Satisfies these requirements

这对我有用。满足这些要求

  1. Accept arbitrary input that may include shell special characters
  2. Does not output escape character, the "\"
  1. 接受可能包含 shell 特殊字符的任意输入
  2. 不输出转义字符,“\”
#! /bin/bash

FOO='myTest3$;  t%^&;frog now! and *()"'

FOO=`printf "%q" "$FOO"`                        # Has \ chars
echo $FOO

# Eat all the \ chars
FOO=$(printf "%q" "$FOO" | sed "s/\\//g")     # Strip \ chars

echo $FOO