Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by 'output to stdout'

Tags:

linux

bash

New to bash programming. I am not sure what is meant by 'output to stdout'. Does it mean print out to the command line?

If I have a simple bash script:

#!/bin/bash
wget -q  http://192.168.0.1/test -O -  | grep -m 1 'Hello'

it outputs a string to the terminal. Does this mean it's 'outputting to stdout' ?

Thanks

like image 871
0xSina Avatar asked Nov 22 '25 11:11

0xSina


1 Answers

Every process on a Linux system (and most others) has at least 3 open file descriptors:

  • stdin (0)
  • stdout (1)
  • stderr (2)

Regualary every of this file descriptors will point to the terminal from where the process was started. Like this:

cat file.txt # all file descriptors are pointing to the terminal where you type the      command

However, bash allows to modify this behaviour using input / output redirection:

cat < file.txt # will use file.txt as stdin

cat file.txt > output.txt # redirects stdout to a file (will not appear on terminal anymore)

cat file.txt 2> /dev/null # redirects stderr to /dev/null (will not appear on terminal anymore

The same is happening when you are using the pipe symbol like:

wget -q  http://192.168.0.1/test -O -  | grep -m 1 'Hello'

What is actually happening is that the stdout of the wget process (the process before the | ) is redirected to the stdin of the grep process. So wget's stdout isn't a terminal anymore while grep's output is the current terminal. If you want to redirect grep's output to a file for example, then use this:

wget -q  http://192.168.0.1/test -O -  | grep -m 1 'Hello' > output.txt
like image 168
hek2mgl Avatar answered Nov 25 '25 10:11

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!