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
Symbols | Role |
---|---|
Command < file | Use file as standard input for commands |
Command << delimiter | Reads from standard input and does not stop until it meets a delimiter |
command < file 1 > file 2 | use file 1 as standard input to the command and standard output to file 2 |
Output redirection
Symbols | Effects |
---|---|
Command > File | Redirects the standard output to a file (clears the data of the original file) |
Command 2> File | Redirects the error output to a file (clears the data of the original file) |
Command >> File | Redirects standard output to a file (appends to the back of the original content) |
Command >> File | Redirects error output to a file (appends to the back of the original) |
Command >> File 2>&1 or Command &>> File | Writes 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
Wildcards | Meaning |
---|---|
* | 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 ( \
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 names | Role |
---|---|
HOME | The user's home directory (i.e. home directory) |
SHELL | The name of the shell interpreter the user is using |
HISTSIZE | The number of command history records output |
HISTFILESIZE | Number of command history records saved |
path to save mail | |
LANG | System language, language name |
RANDOM | Generate a random number |
PS1 | The prompt for the Bash interpreter |
PATH | Defines the path where the interpreter searches for commands to be executed by the user |
EDITOR | The user's default text editor |