Command:
$ echo 'hello' | xargs -I{} sh -c "echo 'hi'/$(echo {}| sed 's/hello/bye/g')"
Expected:
> hi/bye
Actual:
> hi/hello
Note that:
$ sh -c "echo 'hi'/$(echo hello| sed 's/hello/bye/g')"
> hi/bye
I would also highly appreciate the reason behind why the answer works, and why this does not.
EDIT:
$ set -x; echo 'hello' | xargs -I{} sh -c "echo 'hi'/$(echo {}| sed 's/hello/bye/g')"
+-zsh:126> echo hello
+-zsh:126> echo '{}'
+-zsh:126> sed s/hello/bye/g
+-zsh:126> xargs '-I{}' sh -c 'echo '\''hi'\''/{}'
hi/hello
Charles is correct, the echo is evaluated before the xargs substitution
EDIT 2:
The use case is:
find $(pwd) -path "*jpg" | head| xargs -I{} sh -c "ln -s {} $(pwd)/flat/$(echo {} | sed 's/\//_/g')"
Wanting to symlink a bunch of nested files into a flat folder with some name updates