There are some Linux command line special characters that are not self-explanatory at first glance. It is important to understand them because these are key elements in handling the input and output of commands. In this article, I will review the most common special characters.
In a Unix shell, the output of a command is usually sent to standard output (stdout) and error messages to standard error (stderr). Several special characters allow you to modify the output of a command.
Caractère | Description | Exemple |
---|---|---|
| | Redirects the output of one command to another command, as string input. | # Applies the grep command on the output generated by ls ls | grep "file.txt" |
> | Redirect output to a file. Creates the file if it does not exist and overwrites it if it already exists. | # Create file.txt, containing the list of files returned by ls ls > file.txt |
>> | Redirect output to a file. Create the file if it does not exist, complete it if it already exists. | # Creates the file.txt file then completes it from the disk space information (df) ls > file.txt ; df >> file.txt |
>| | Forces standard output to be redirected to a file, even if the file is read-only | # Write file "file.txt" even if it already exists read-only echo "Hello" >| "file.txt" |
>! | Redirects standard output to an existing file, even if the file is read-only. This function avoids read-only security warnings, compared to the ">|" function. | # Creates/replaces "file.txt" with the contents of the directory even if the file is read-only. However, the file must already exist. ls >! "file.txt" |
2> | Redirects standard errors (stderr) of a command to a file, and replaces it if it already exists. Pour ne pas afficher le message d'erreur système, on peut le diriger vers le flux null (/dev/null), qui est une sorte de poubelle pour les données. | # Displays the contents of a file. As it does not exist, write the error generated by the command in the error.txt file cat "unexist.txt" 2>"error.txt" # If the file exists, its content is sent to file2.txt, otherwise the error is sent to error.txt cat "file.txt" > "file2.txt" 2>"error.txt" # If the directory does not exist, does not display the system error ls /unexist 2>/dev/null |
2>> | Redirects standard errors (stderr) from a command to a file, and completes it if it already exists. | # By launching the command twice, the error is recorded twice in the error.txt file cat "unexist.txt" 2>>"error.txt" |
&> or >& | Redirects standard output (stdout) and standard errors (stderr) to a file, and overwrites it if it already exists. | # If file.txt exists, file2.txt will contain the same text. Otherwise it will contain the error generated by the "cat" command cat "file.txt" &> "file2.txt" |
&>> | Redirects standard output (stdout) and standard errors (stderr) to a file, and completes it if it already exists. | # Creates the file "file2.txt" with the contents of the directory, then completes it with the same contents of the directory again ls > "file2.txt" ; ls &>> "file2.txt" |
>( command ) | Redirects the standard input of a command from the output of another command as a temporary file. This command can work from a list of files. Note that this syntax is called "process substitution operator" and can be used on large outputs, in order to use a temporary file. | # Redirect the input from the "ls" command to the input of the "grep" command, using a temporary file ls > >(grep "file.txt") |
<( command ) | Redirects the standard output of one command to the input of another command as a temporary file. | # Outputs "a b" and "a c" are sent to the "diff" command diff <(echo -e "a\nb") <(echo -e "a\nc") |
2>&1 | Redirect error output to standard output. | # The grep command receives the contents returned by "ls", either the file or the error "No such file or directory" ls "unexist.txt" 2>&1 | grep "No such file" |
|& | Redirect both standard errors and standard output to the following command. The main difference with the previous command is that this one creates an underlying process to handle the redirection of the two outputs (in some cases this may cause performance or compatibility issues). | # The grep command receives the contents returned by "cat", either the file or the error "No such file or directory" cat "unexist.txt" |& grep "No such file" |
Command entry can be manipulated using a few special characters.
Caractère | Description | Exemple |
---|---|---|
< | Redirects command input from a file. | # Displays on the output the sorted contents of the file sort < "file.txt" # An equivalent cat "file.txt" | sort |
<<< | Redirects command input from a character string. | # Search for "b" in the string "a b c" grep "b" <<< "a b c" |
<< | Redirects command input from a multiline, delimited string. | # Display "Hello, world!" with a carriage return between the two words cat << BEGIN Hello, world! BEGIN |
Other special characters have functions unrelated to input-output, such as chaining conditions.
Caractère | Description | Exemple |
---|---|---|
& | Launches a process in the background (without blocking the terminal). | # Waits 60 seconds in task background, without blocking the terminal sleep 60 & |
; | Execute another command after the first has completed, whether it was successful or not. | # Places in the /opt directory then lists the files cd /opt ; ls |
&& | Execute several commands consecutively, only if the previous one was successful. | # Places in the /opt directory and lists the files only if it exists cd /opt && ls |
|| | Execute several commands consecutively, only if the previous one failed. | # Places in the /unexist directory and lists the files only if the command failed (an error message is displayed by the command in error) cd /unexist || echo "Erreur !" # Runs the same command but redirects standard error to /dev/null cd /unexist 2>/dev/null || echo "Erreur !" |
!! | Reruns the last executed command. | # Executes the display of the current directory twice ls ; !! |
RSS | Informations |