So, I was playing around with this answer, when I found that neither
printf_stdin () {
read input
printf "$input"
}
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\(lorem ipsum \)\(foobar\)/\1'\"\$(printf '%s' '\2' | printf_stdin)\"'/g
s/.*/printf '%s' '\0'/e
}" <<< "lorem ipsum foobar"
nor
printf_ () {
printf "$1"
}
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\(lorem ipsum \)\(foobar\)/\1'\"\$(printf_ '\2')\"'/g
s/.*/printf '%s' '\0'/e
}" <<< "lorem ipsum foobar"
works. They both output the error sh: 1: <bash function>: not found. I'm not planning to use this thing anymore, but still, I'm curious why this doesn't work and how to fix it.
Edit
Running
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\(lorem ipsum \)\(foobar\)/\1'\"\$(printf '%s' '$0')\"'/g
s/.*/printf '%s' '\0'/e
}" <<< "lorem ipsum foobar"
gives
lorem ipsum /bin/bash
so sed seems to be invoking the bash shell and not the bourne shell?
/bin/shbashor is itdash? Or something else?echo bar | perl -pe 'BEGIN{sub myfoo {my $str = shift; return "foo $str"}}; s/.*/myfoo $&/e'(sure, this trivial example doesn't need the /e modifier OR a function, but it does illustrate what's possible and how much easier it is than doing it in shell). people try to do too much in shell, a language that makes easy things needlessly difficult./bin/sh? Is it perhaps a symlink tobash? Does this work if you runexport -f printf_stdinbefore your first command?