Data streams (Bash)
A stream or datastream is something that can transfer data. Whenever running a command in Bash, the three default data streams, stderr, stdin and stnout are activated. They all three carry data. Amongst other things, streams are relevant for piping and redirection. Streams have endpoints, and these enables them to connect commands to each other and/or to files.
The three basic streams - They all use text:
- stdin: Standard input
- stout: Standard output
- sterr: Standard error.
Like lots of things in Unix and Linux, they correspond with files and devices.
Examples:
- stdin: When using
read
for input from the user, stdin is used to capture what is typed - stout: When using
ls
, stout sends the content of the directory to the console - sterr: When generating an error, sterr sends the error message from the interpreter (or whatever) to the screen.
Piping
Use the |
sign for piping, or redirecting stdout of one command, with stdin of another one:
ls | grep 120
To redirect both stdout and stderr to stdin of the next command, use |&
.
Redirect stdout to a file
E.g.:
ls > dir.txt
Redirect stdout to a file - With append function
Use >>
rather than >
to append the stdout stream to a file:
ls >> dir.txt
Redirect stdin - Example
cat is used for concatenating files and sending their content to stdout.
E.g, use
cat filtering.sh
to get the contents of this file to the screen. This is the 'normal' use of the command: It expects a file name as argument, it gets its, and it does its usual magic with it.
Now we want to redirect stdin: Rather than that it comes from a file, we would like to provide a string.
This would be my first choice, but it doesn't work:
$ "Hello, world!" > cat bash: Hello, world!: command not found
I think the reason is quite practical: Bash doesn't know what to do when a command line starts with a file name. That's actually where I believe cat or echo are often used for: As a placeholder or a dummy in stead of a file name.
Back to the problem: We need to start the command line with a command, and this is how we can fix it:
cat < "Hello, world!"