Unix命令转义空格 [英] Unix command to escape spaces

查看:91
本文介绍了Unix命令转义空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Shell脚本的新手.有人可以用命令帮助我用"\"转义空格. 我有一个变量FILE_PATH =/path/至我的文本文件,
我想一个人逃脱 FILE_PATH =/path/to \ my/text \文件

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

我尝试使用tr -s命令,但没有帮助

I tried with tr -s command but it doesnt help

FILE_PATH = echo FILE_PATH | tr -s " " "\\ "

FILE_PATH=echo FILE_PATH | tr -s " " "\\ "

有人可以建议正确的命令!

Can somebody suggest the right command !!

推荐答案

如果您使用的是bash,则可以使用其内置的printf的%q格式化程序(在bash中键入help printf):

If you are using bash, you can use its builtin printf's %q formatter (type help printf in bash):

FILENAME=$(printf %q "$FILENAME")

这不仅会引用空格,还会引用shell的所有特殊字符.

This will not only quote space, but also all special characters for shell.

这篇关于Unix命令转义空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆