SUSE linux
bash shell
this works
test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /"
prefix directory doesn't exists
but why doesn't this work?
test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /"
directory exists
most likely because /tmpp != /tmp
If you want it to work, both echo statements have to be part of one single process, I chose a subprocess:
( test -d /tmp && echo "directory exists" || echo "directory doesn't exists" ) | sed -e "s/^/prefix /"
prefix directory exists
Otherwise the part after the ||
is part of the second compound statement (echo )not the first.
5 Likes
I guess even a { code block; } is forced into a subshell because of the pipe.
An if-then-fi is a code block, too. The following is unusual but standard
if test -d /tmp; then
echo "directory exists"
else
echo "directory doesn't exist"
fi | sed -e "s/^/prefix /"