Home > database >  why does "echo "qwerty" | /bin/sh" return "/bin/sh: 1: qwerty: not found&qu
why does "echo "qwerty" | /bin/sh" return "/bin/sh: 1: qwerty: not found&qu

Time:11-25

I know that echo command displays the line of text that is passed as argument.

So the syntax echo "qwerty" would display:

qwerty

but when I merge the previous syntax with | /bin/sh the following message is displayed:

/bin/sh: 1: qwerty: not found

I would like to know why using bitwise OR operator (i.e. | ) this way ending up with such an output.

CodePudding user response:

| is not a bitwise OR operator.[1] It's a pipe operator. It causes the stdout of the preceding program to be piped to the stdin of the following program.

$ printf 'abc def\nghi\n' | wc
      2       3       12

This shows wc ("word count") reading the output of printf and printing out the fact that it received 2 lines, 3 words and 12 bytes.

In your case, sh reads its stdin for commands (due to the absence of both a -c option and a file name argument), and thus treats qwerty as a command to execute.


  1. It can be bitwise OR in arithmetic context when using bash and possibly other shells in the "sh family". That's not the case here even if you were using bash.
  • Related