mkdocs-pages/docs/Linux/shellbasics.md

3.2 KiB

Basics

Basics of the Linus Shell

Streams

The linux shell (this will usually be bash) 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)

STREAM ID   STREAM NAME 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 program it is also how we interact with the command line shell (Usually bash)

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 program 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 program 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, this is called a pipe 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'

Standard Error

Standard error is how error messages are sent between programs or displayed to the user

As this is a different to STDOUT you will still get output to the terminal if you redirect using the '>' symbol

Error Redirection

Errors can be redirected using the following '2>' this specifies the stream to redirect and the direction

For example you can redirect errors of an program to an error log file using '2> error.log' so a command that uses this would look like find / -name "filename" 2> errors.log this would make an error.log in the directory you are in and save the errors in a log and show the results from STDOUT on the screen (In this example the errors that you would see are Permission Denied errors)

More redirection tips

You can do quite a bit with piping and redirection, using these basics you will be able to do quite a bit in the bash shell I have created some examples both to show how programs can work together and provide some real world examples on what it can be used for here