sh -c doesn't recognize heredoc syntax

Advertisements

I think the two commands below should be identical, but given the heredoc, the shell produces an error. Is it possible to pass a heredoc to the -c argument of sh?

heredoc example

/bin/sh -c <<EOF
echo 'hello'
EOF

# ERROR: /bin/sh: -c: option requires an argument

simple string example

/bin/sh -c "echo 'hello'"

# prints hello

>Solution :

The commands are not equivalent.

/bin/sh -c <<EOF
echo 'hello'
EOF

is equivalent to

echo "echo 'hello'" | /bin/sh -c

or, with here-string:

/bin/sh -c <<< "echo 'hello'"

but sh -c requires an argument. It would work with

echo "echo 'hello'" | /bin/sh

Leave a ReplyCancel reply