加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

传递SED替换BASH变量中的参数

发布时间:2020-12-14 01:28:25 所属栏目:Linux 来源:网络整理
导读:我正在尝试使用BASH将带有空格的变量传递给sed,并且在提示符下,它可以正常工作: $tmp=/folder1/This Folder Here/randomfile.abc$echo "$tmp" | sed -e 's/ / /g'/folder1/This Folder Here/randomfile.abc 但是一旦我将它传递给变量,sed就不再用反斜杠
我正在尝试使用BASH将带有空格的变量传递给sed,并且在提示符下,它可以正常工作:

$tmp=/folder1/This Folder Here/randomfile.abc
$echo "$tmp" | sed -e 's/ / /g'
/folder1/This Folder Here/randomfile.abc

但是一旦我将它传递给变量,sed就不再用反斜杠替换空格:

$tmp=/folder1/This Folder Here/randomfile.abc
$location=`echo "$tmp" | sed -e 's/ / /g'`
$echo $location
/folder1/This Folder Here/randomfile.abc

我希望第二双眼睛可以接受我不喜欢的东西.

解决方法

你需要几对反斜杠:

sed -e 's/ /\ /g'

您似乎想引用输入以便将其用作shell输入.没有必要使用sed.你可以使用printf:

$foo="a string with spaces"
$printf "%q" "$foo"
a string with spaces

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读