Using Redirection, Operators, And Other Useful BASH Tricks

In this post I will be showing you how to use redirection and some other useful BASH tricks. So lets get started by jumping directly into the terminal.

How To Use “|”

You can use the above symbol to pipe a command into another command. For example you may want to pipe the output of smartctl (show your hard drives smart data) directly into grep so that you only get the information you are actually looking for. An example of a pipe in action is below.

smartctl -HAi /dev/sda | grep -iE "serial|status"

How To Use “||”

Two pipe symbols together will be interpreted as an “or”, as in complete the previous command successfully or run this instead. This can come in handy for displaying error messages in your scripts. Here is an example of || in use.

ls /this/directory/doesnt/exist || echo "Directory doesn't exist"

How To Use “>”

Using one greater than symbol will redirect your standard output into whatever file you point it towards. Using only one greater than symbol will overwrite whatever the contents of that file is (if there is any content). This file doesn’t need to exist for this to work, however the directory in which the file will be created or overwritten does need to exist. Here is an example of using the greater than symbol.

echo "hello world" > /root/text.txt

You can also use this to redirect your errors or standard output to /dev/null (meaning they will not be displayed), or any actual file like above. Here are 2 examples, the first one will just redirect the standard output to /dev/null, the second will redirect errors to a log file.

# Ignore standard output
~/scripts/ignore.sh > /dev/null

# Ignore only errors
~/scripts/debug.sh 2> /path/to/log.txt

How To Use “>>”

When using two greater than symbols, you get somewhat the same result as only using one. The key difference is that your output will be appended or added to the end of the existing file (if it exists) without erasing any of the current content. This is great for creating logs while scripting. You can also redirect your errors by using this method, simply use “2>>” instead of “2>”. Here is an example of using 2 greater than symbols adding content to a file.

echo "hello" >> /root/text.txt
echo "world" >> /root/text.txt

How To Use “&”

You can actually use a single & symbol for 2 completely different functions. The first one would be chaining commands. Note that this doesn’t care if the prior command was successful or not, it will run the next command regardless. Here is an example.

echo "hello" & echo "world"

The second thing you can use the single & symbol for is to start your processes/scripts/etc in the background. This is great if you are running something that doesn’t need any interaction but will take a long time to complete because you can continue using your terminal without having to start another session. Here is an example of starting a background process. (Quick tip: you can bring your job back to the foreground by using the command “fg”.)

~/scripts/longtime.sh &

How To Use “&&”

You can use a && to do the same as a single, although this will not create a job in the background. The difference between using && and & is that the double ampersand will only run the next command if the prior one finished successfully. This means that if your first command is curling a script for example and that fails, then the second command will not be run. Here is an example of using &&.

curl https://somesite.com/script.sh && chmod +x script.sh

Combining The Above

You can also combine the above to do some interesting things. For example we can combine && and || to create the following logic. Do this and if successful then do this as-well, or if unsuccessful then do this instead. Here is an example of the logic above.

cp /path/to/log.txt ~/backups/log.txt && echo "Log file was backed up" || echo "Log file was not backed up"

Bash Tricks

Below are some of the more convenient and useful shortcuts and tricks you can use if you are using BASH as your shell.

  • When entering the path to a file or even a filename, press tab to see available files or auto complete if only 1 potential match.
  • Press CTRL+L to clear the terminal screen.
  • Press CTRL+D to disconnect from an ssh session.
  • Press CTRL+C to cancel a process.
  • Press CTRL+Z to pause/suspend a process and place it in the background.
  • You can use bash loops directly in the terminal, no need to save as a script.

I hope you enjoyed this post, please feel free to like, comment, or share. Thank you for the support!

Leave a Reply

Your email address will not be published. Required fields are marked *