39 lines
2.1 KiB
Markdown
39 lines
2.1 KiB
Markdown
# Basics of the linux shell
|
|
|
|
## Streams
|
|
|
|
The linux shell (this will usually be [`bash`](bash.md)) works using different streams
|
|
|
|
The main three streams that are used on the command line are STDIN (Standard input), STDOUT (Standard output), STDERR (Standard Error)
|
|
|
|
|ID|STREAM|USE|
|
|
|---|:----|:---|
|
|
| 0 | STDIN | Used to send input to the program |
|
|
| 1 | STDOUT | Used to receive output from the program |
|
|
| 2 | STDERR | Used to receive errors from the program |
|
|
|
|
### Standard Input
|
|
|
|
Standard input is how we send input to an application it is also how we interact with the command line shell (Usually [`bash`](/bash.md))
|
|
|
|
Whenever a linux program asks for input this is sent to the program using the STDIN stream
|
|
|
|
The STDIN stream can also be refrenced by the '-' char in a command line application `cat -` will print what comes into STDIN (The terminal)
|
|
|
|
One thing that makes the linux command line so powerfull is the suite of commands that is provided all work using STDIN, STDOUT and STDERR you can pipe information from one stream to another (i.e the STDOUT from one program can be in STDIN to another)
|
|
|
|
#### Input Redirection
|
|
|
|
You can use redirection to send information to a program using the '<' symbol i.e `cat < file` this will push the contents of 'file' to cat using STDIN
|
|
|
|
### Standard Output
|
|
|
|
Standard output is how we get the output from a program, usually this is printed directly to the terminal
|
|
|
|
#### Output Redirection
|
|
|
|
You can redirect the STDOUT of a application is a few ways you can use the '>' symbol to push the information from the programs output and saves it in a file for example `ls > filelist` will take the output of ls and saves it in the filelist file
|
|
|
|
You can also redirect the STDOUT of one program to another programs STDIN using the '|' symbol doing this you can combine muliple programs in different ways
|
|
|
|
For example you can use `ls -alh | grep filename` this will list the files in the current directory and then passes the STDOUT to the STDIN for grep, this causes grep to use the output of ls as the input data to work with so the output of ls is searched for the string 'filename' |