J. R. Swab

Learn Bash: Tutorial 003

Categories: [Technology]
Tags: [open-source], [linux], [bash]

The previous post in this series covered a few basic commands that we use most often in both the terminal and in Bash scripts. Today we will stay with this trend as we progress into more ways to manipulate what our computer does. If you have never used Bash, go back and read over the other two posts.

As said in the last post if you are reading this and you know bash please help by filling in any gaps in thought via the comment section. The goal is to keep these about one thousand words long and there may be parts I brush over. So please expand anything that will help a new bash user.

I will add an edit to the main content and link to each comment I feel points out a useful tip or expands upon a thought in more detail. Those comments will also be up-voted by myself to keep them near the top of the trending list.

Also, thanks to everyone who has added great points in the past two post. You all are making this series as in depth as it can be. I kind of wish I had this when first learning Bash! The more we work together the better off new users will be and that is what it is all about.

Redirect Data

Output

As we have seen in the last two posts when we run a command Bash will output the results to the terminal screen. This is great but sometimes we want to have that information saved. As with most of this series so far, it is easy to do. The > is what we use and the command will look something like this:

cat someFile.txt > someOtherFile.txt

If we use cat without the > Bash will dump the contents of someFile.txt into the terminal. However, we told the command to save that output to someOtherFile.txt. This will overwrite anything in the second file so be careful when using >. We don't want any accidents.

What if we want to add the output of a command to a file but not over write the entire file? This is done by using >> instead of only one >. Which gives us cat someFile.txt >> someOtherFile.txt and will append the contents of someFile.txt to the second file. This is useful when adding information to a log file or when we need to combine two documents.

This is for any command that writes to "standard output". Most commands in Bash use standard output but some write to what is known as "standard error". To catch both sets of outputs and append the information to a file, we can use &>>. The command above then looks as follows:

cat someFile.txt &>> someOtherFile.txt or we can use cat someFile.txt > someOtherFile.txt 2>&1.

I prefer the first since it seems easier for me to remember. Use what is easiest for you. Now we can save two different outputs to a file but we can also save to two separate files.

To save the standard output to one file and the standard error to another we can use >> and 2>> together. What we get is something like

cat someFile.txt >> someOtherFile.txt 2>> errorFile.txt

Not too hard to remember and we can even tell Bash to only save the standard error information by using cat someFile.txt 2>> errorFile.txt.

Input

We can also redirect input from a file to a command. The command looks like cat < someFile.txt. This is redundant for the cat command since doing cat someFile.txt has the same outcome. However, there may be a day were a Bash command we need to use does not act similarly to cat. In that case we will need to use this redirection method.

One way this can be useful is when there is a lot of data in a file we need to added to a command. It will save us time to redirect the file's data into the command using <. The more we learn and use Bash the more we look for the fastest way to accomplish a task.

Keep in mind that the way cat works without < is a normal feature of most utilities. The few times we need to use this are exceptions to the rule. We may find we never use input redirection but as with all knowledge bases out there it is good to know.

Introduction To Pipes

We all peak at our keyboard from time to time and we may have see that straight line that shares a key with the backslash. | is the pipe and we can use it in Bash to use the information from another command. We use this as the separator in a line of multiple commands we wish to use together.

This group of commands is knows as a pipeline. We will get much deeper with this handy tool in a future post so let's only touch on it lightly here. When we pipe two or more commands together they all run at the same time and use the output from the previous command as data.

'cat stuff.txt | grep jrswab | grep -v "is cool"`

The above command works as follows. We know cat will display the contents of stuff.txt. That data then gets piped into grep (grep is a Bash command that allow us to search plain text). The first grep then searches the output of cat stuff.txt for all lines that contain 'jrswab'.

Once found the second grep then searches the first for all lines that do not contains "is cool". What we receive on the screen are the lines of stuff.txt that contain "jrswab" but not "is cool". This is useful when we need to look through a large file of text but only need to see a small set of data.

Remember the extension does not matter and this will work on any text file such as Python files, plain text files, HTML files, ect.


Ways to support the blog.

If you are an email kind of nerd you can sign up for mine here. You can donate to this site from my Liberapay account if you so choose. If you want a more passive way to support this site, use this link when shopping on Amazon; it kicks some of Amazon's profit to me at no extra cost to you.