2

I'm new to unix. I typed this command in ubuntu terminal:

pwd | echo

I expected to see the output of pwd in terminal(/home/fatemeh/Documents/Code/test) but the output was just a single empty line.

why this happens?

marked as duplicate by G-Man, Kusalananda, roaima, Community Jul 27 '18 at 15:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

echo does not do anything with standard input; it only parses its parameters. So you are effectively running echo which, by itself, outputs a single empty line, and the standard input is discarded.

If you want to see the behavior you are trying to implement, use a tool designed to parse standard input, such as cat:

$ pwd | cat
/home/username

If you really want to use echo to display the current working directory (or the output of another command), you can use Command Substitution to do this for you:

$ echo "Your shell is currently working in '$(pwd)'."
Your shell is currently working in '/home/username'.
2

As is stated in the other answers, echo does not read from stdin so it will not print the output of pwd. Just in case you don't know already, pwd prints it's output on it's own so you simply need to run it alone to get your desired result.

$ pwd
/current/working/dir

If you really want to use echo for this you can use Command Substitution. This will pass the output of your command (pwd) to echo as a parameter. Which again in this example is not necessary as pwd will output it's own...output to stdout.

$ echo "$(pwd)"
/current/working/dir
1

The echo command doesn't read from standard input, it writes what you tell it to write. In this case, you didn't tell it to write anything, so all it printed was a newline. pwd | echo is equivalent to just running echo.

If you want to use a pipe, then you need to use some command that reads from standard input (e.g., cat):

$ pwd | cat
/path/to/current/directory
$ 

Not the answer you're looking for? Browse other questions tagged or ask your own question.