Skip to main content

Pipe characters, redirects and environment variables

Input and Output Redirection

Input redirection is the import of a file into a command, while output redirection is the writing of data information that would otherwise be output to the screen to a specified file.

  • Standard input redirection (STDIN, file descriptor 0): input from the keyboard by default, but also from other files or commands.

  • Standard output redirection (STDOUT, file descriptor 1): output to the screen by default.

  • Error output redirection (STDERR, file descriptor 2): default output to screen.

File descriptor 1 can be omitted for standard output mode in redirection, while file descriptor 2 is mandatory for error output mode.

Input redirection

Symbols and their effects

SymbolsRole
Command < fileUse file as standard input for commands
Command << delimiterReads from standard input and does not stop until it meets a delimiter
command < file 1 > file 2use file 1 as standard input to the command and standard output to file 2

Output redirection

SymbolsEffects
Command > FileRedirects the standard output to a file (clears the data of the original file)
Command 2> FileRedirects the error output to a file (clears the data of the original file)
Command >> FileRedirects standard output to a file (appends to the back of the original content)
Command >> FileRedirects error output to a file (appends to the back of the original)
Command >> File 2>&1 or Command &>> FileWrites standard output to a file with error output (appends to the back of the original)

Pipeline command character

The pipe character can be entered by pressing Shift + backslash () on the keyboard and is executed as "Command A | Command B". The Pipeline command takes the information that would have been output to the screen by the previous command and uses it as standard input for the next command.

# Output the number of lines for the user who is banned from logging in
grep /sbin/nologin /etc/passwd | wc -l

# Search for processes related to bash
ps aux | grep bash

Command characters can be infinite combinations, for example: command A | command B | command C | ......

A particularly apt analogy was once made by a student from the north-east: treating pipe characters like an assembly line operation is the same as having a barbecue, i.e. the first person is responsible for cutting the meat, the second for skewering it, the third for grilling it, and the final result is delivered to the user.

Wildcards on the command line

Wildcards and their meanings

WildcardsMeaning
*Any character
?single arbitrary character
[a-z]a single lowercase letter
[A-Z]single uppercase letter
[a-Z]single letter
[0-9]single digit
[[:alpha:]]arbitrary letters
[[:upper:]]any upper case letter
[[:lower:]]any lowercase letter
[[:digit:]]all digits
[[:alnum:]]any letter plus a number
[[:punct:]]punctuation

Example.

# Match files in the /dev directory that start with sda
ls -l /dev/sda*

Commonly used escape characters

  • Backslash (): makes a variable after a backslash a mere character.

  • single quotes (' '): escapes all variables in it to mere strings.

  • Double quotes (" "): retains the attributes of the variables in it without escaping them.

  • backquote ( \ ): returns the result of executing the command in it.

Important environment variables

In Linux, it is a convention that variable names are generally in upper case and commands are in lower case.

The 10 most important environment variables in Linux

Variable namesRole
HOMEThe user's home directory (i.e. home directory)
SHELLThe name of the shell interpreter the user is using
HISTSIZEThe number of command history records output
HISTFILESIZENumber of command history records saved
MAILpath to save mail
LANGSystem language, language name
RANDOMGenerate a random number
PS1The prompt for the Bash interpreter
PATHDefines the path where the interpreter searches for commands to be executed by the user
EDITORThe user's default text editor