Linux 转义空格的 Unix 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12806987/
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
Unix command to escape spaces
提问by Lolly
I am new to shell script. Can someone help me with command to escape the space with "\ ".
I have a variable FILE_PATH=/path/to my/text file ,
I want to escape the spaces alone
FILE_PATH=/path/to\ my/text\ file
我是 shell 脚本的新手。有人可以用命令帮助我用“\”转义空格。我有一个变量 FILE_PATH=/path/to my/text file ,
我想单独转义空格 FILE_PATH=/path/to\ my/text\ file
I tried with tr -s command but it doesnt help
我试过 tr -s 命令,但它没有帮助
FILE_PATH=echo FILE_PATH | tr -s " " "\\ "
文件路径=echo FILE_PATH | tr -s " " "\\ "
Can somebody suggest the right command !!
有人可以建议正确的命令!!
回答by William Pursell
There's more to making a string safe than just escaping spaces, but you can escape the spaces with:
使字符串安全的不仅仅是转义空格,但您可以使用以下方法转义空格:
FILE_PATH=$( echo "$FILE_PATH" | sed 's/ /\ /g' )
回答by Gilles Quenot
You can do it with sed
:
你可以这样做sed
:
NEW_FILE_PATH="$(echo $FILE_PATH | sed 's/ /\ /g')"
回答by hymie
FILE_PATH=/path/to my/text file
FILE_PATH=echo FILE_PATH | tr -s " " "\ "
That second line needs to be
第二行需要是
FILE_PATH=echo $FILE_PATH | tr -s " " "\ "
but I don't think it matters. Once you have reached this stage, you are too late. The variable has been set, and either the spaces are escaped or the variable is incorrect.
但我认为这不重要。一旦你到了这个阶段,你就晚了。变量已设置,空格被转义或变量不正确。
FILE_PATH='/path/to my/text file'
FILE_PATH='/路径/到我的/文本文件'
回答by Bao Haojun
If you are using bash, you can use its builtin printf's %q formatter (type help printf
in bash):
如果您使用的是 bash,则可以使用其内置 printf 的 %q 格式化程序(输入help printf
bash):
FILENAME=$(printf %q "$FILENAME")
This will not only quote space, but also all special characters for shell.
这不仅会引用空格,还会引用 shell 的所有特殊字符。
回答by jahroy
You can use 'single quotes' to operate on a path that contains spaces:
您可以使用“单引号”对包含空格的路径进行操作:
cp '/path/with spaces/file.txt' '/another/spacey path/dir'
cp '/path/with spaces/file.txt' '/another/spacey path/dir'
grep foo '/my/super spacey/path with spaces/folder/*'
grep foo '/my/super spacey/path with spaces/folder/*'
in a script:
在脚本中:
#!/bin/bash
spacey_dir='My Documents'
spacey_file='Hello World.txt'
mkdir '$spacey_dir'
touch '${spacey_dir}/${spacey_file}'
回答by sureshvv
Use quotes to preserve the SPACE character
使用引号保留空格字符
tr is used only for replacing individual characters 1 for 1. Seems to be that you need sed.
tr 仅用于将单个字符 1 替换为 1。似乎是您需要 sed。
echo $FILE_PATH | sed -e 's/ /\ /'
seems to do what you want.
似乎做你想做的。
回答by kikiwora
Do not forget to use evalwhen using printf guarding.
使用 printf 保护时不要忘记使用eval。
Here's a sample from Codegen Script under Build Phases of Xcode (somehow PROJECT_DIR is not guarded of spaces, so build was failing if you have a path with spaces):
这是 Xcode 构建阶段下的 Codegen 脚本示例(不知何故 PROJECT_DIR 不受空格保护,因此如果您的路径带有空格,则构建失败):
PROJECT_DIR_GUARDED=$(printf %q "$PROJECT_DIR")
eval $PROJECT_DIR_GUARDED/scripts/code_gen.sh $PROJECT_DIR_GUARDED