bash 如何在没有带环境变量的文件的情况下使用 SED?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8903180/
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:20:11  来源:igfitidea点击:

How to use SED without a file with an env var?

bashshellsedenvironment-variables

提问by hhh

Is it possible to substitute a thing in env var with SED?

是否可以用 SED 替换 env var 中的内容?

$ a='aoeua'
$ sed 's@a@o@g' <$a
bash: aoeua: No such file or directory
$ env|grep "SHELL"
SHELL=/bin/bash

The output I want is

我想要的输出是

ooeuo

replacing each ain 'aoeua'with o.

代替每个a'aoeua'o

回答by Laurence Gonsalves

Use echo:

使用回声:

$ echo "$a" | sed 's@a@o@g'

In bash you can also do simple substitutions with the ${parameter/pattern/string}syntax. For example:

在 bash 中,您还可以使用${parameter/pattern/string}语法进行简单的替换。例如:

$ v='aoeua'
$ echo ${v/a/o}
ooeua

Note that this only replaces the first occurrence of the pattern.

请注意,这仅替换第一次出现的模式。

回答by potong

This might work for you:

这可能对你有用:

a='aoeua'
sed 's@a@o@g' <<<$a
ooeuo

<<<$ais a here-string

<<<$a是一个here-string