From LQWiki
Jump to: navigation, search

stdout is the standard output stream, usually directed to the user's terminal, though it can also be directed to a file or another program.

By convention, text that is printed normally at a console goes through stdout including prompts and reports of bad interactive input, though if an internal error occurs, the program may (the program should) report the message to standard error.

Different shells have different capabilities and use different syntax with respect to the standard file streams. All that follows is correct for bash but may differ somewhat in other shells.

stdout can be directed into a file using the > operator:

ls >file1

puts the output of ls into file1

The content of stdout can be stored in a variable using backticks (`):

scorpio:~ # export v=`echo hallo`
scorpio:~ # echo $v
scorpio:~ #   

The output of echo has been placed into the variable $v.

The content of stdout can be stored in a variable using the cascadable $() operator:

scorpio:~ # export v=$(ll $(ls /bin/bash))
scorpio:~ # echo $v
-rwxr-xr-x 1 root root 501804 Apr 23 03:46 /bin/bash
scorpio:~ # 

The content of stdout can be redirected to stdin of another program using a pipe:

scorpio:~ # echo "hello" | sed 's/hello/hi/'
scorpio:~ # 

Note that, in this example, echo does not write to the console, but to the input of sed. And sed does not take its input from the keyboard, but from the output of echo. sed 's/hello/hi/' means: replace "hello" by "hi".

See also