Bash 引用当前路径 (pwd)
我遇到了 PWD 上发生的最烦人的问题当当前路径包含空格时变量。我的代码看起来有点像这样:
mycommand |sed -E '
s|mystuff|replacement| ;
s|'$(pwd)'|replacement| ;
'
除非当前路径包含空格字符,否则效果很好。如果是这样, $(pwd)
会扩展为
'mypath/with space'
而不只是
mypath/with space
这会导致 sed 表达式混乱(因为有额外的引号):
sed: 1: "s|mypath/with": unterminated substitute pattern
我注意到它对扩展 pwd 没有帮助像这样:${PWD//\'/}
。
如何解决这个问题?
I have encountered a most annoying problem that occurs on the PWD variable when the current path includes a space. My code looks somewhat like this:
mycommand |sed -E '
s|mystuff|replacement| ;
s|'$(pwd)'|replacement| ;
'
This works great, unless the current path contains a space character. If it does, $(pwd)
is expanded to
'mypath/with space'
instead of just
mypath/with space
This cause the sed expression to be messed up (because of the extra quotes):
sed: 1: "s|mypath/with": unterminated substitute pattern
I have noticed that it doesn't help to expand pwd like this: ${PWD//\'/}
.
How can this be solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将单引号替换为双引号,并将引号替换为 pwd 周围的反引号:
双引号允许扩展变量和反引号命令。
Replace single quotes by double quotes and replace quotes with backquotes around pwd:
Double quotes allow expansion of variables and backquoted commands.
替换
为
它看起来像这样:
Replace
with
It would look like this then: