简体   繁体   中英

sed command evaluation function not working

i have this function

function test { echo $1; }

i have this sed expression

echo "hello" | sed "s/hello/test world/e"

expected result was

"world"

but it gives the result

sh: test: command not found

my OS: linux fedora 20, sed (GNU sed) 4.2.2.

any solving idea ?

Shell functions are only available in the current shell.

They can not be called via external commands like sed , env , sudo , find or xargs :

$ foo() { echo "hello world"; }

$ foo
hello world

$ sed -e 's/.*/foo/e'
sh: 1: foo: not found

$ env foo
env: ‘foo’: No such file or directory

If you want to run shell commands from such a context, put them in a separate script so you can invoke that instead.

Bash supports exporting functions to child bash instances, but dash in particular will strip them from the environment, so they can't be used for sed which invokes sh -c , even if you end up running a second bash instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM