Redirect operators in Linux and Unix (macOS)
Photo by Isaque Pereira on pexels.com

Redirect operators in Linux and Unix (macOS)

Note: this post was originally published on my personal blog mariadcampbell.com.

In order to understand what redirect operators do in Linux or Unix (macOS), one first has to understand what stdin and stdout mean.

In the case of a personal Linux (or macOS) device, for example, by default, stdin refers to a device's keyboard, and stdout refers to Command Line (Terminal). And these pathways are called streams.

That being said, we don't always want to output stdout to Command Line, which is not persistent by default. Perhaps we want to save it somewhere. It is possible to change our default input and output locations. This means making the OS to obtain information from somewhere other than the stdin or send results to somewhere other than the stdout. This functionality is known as redirection.

The output redirector operator (>)

The default output redirector operator is the most popular and well known of redirectors. As previously mentioned, by default, when not using redirection, stdout is made in Terminal. However, this output is not persistent because it is not saved to Terminal! If it was, we would have an awful lot of clutter there!

In Linux Command Line, if I were to run the following in Terminal:

$HOME        

the following would be returned in Terminal:

bash: /home/maria: Is a directory        

I type $HOME with my laptop keyboard, my stdin, and it is output to the Terminal window, my stdout. But it is not saved anywhere, so I can't refer to that particular output later. But as also previously mentioned, I can redirect this output somewhere else, and even save the data using the output redirector (>). So let's say I run the following command instead:

printenv 'HOME' > home.txt        

Then, if I use the cat command to output the contents of home.txt:

cat home.txt        

It results in the following output in Terminal:

/home/maria        

However, if I simply ran the following without using the output redirector operator:

printenv 'HOME'        

the following would be returned in Terminal:

/home/maria        

And /home/maria would not be redirected and output to home.txt.

The printenv command prints the value of an environment variable, and $HOME is one of them. In this case, printenv is printing the value of the global environment variable called 'HOME'. And since I am not appending $ to the beginning of HOME, it is not being treated as a variable containing a value (HOME=/home/maria), but simply as the value of $HOME.

The input redirector operator (<)

The default input redirector (<) redirects stdin somewhere else. For example, if I type the following in Terminal followed by the Return (Enter) key:

printenv 'HOME' < home.txt        

this would result in the following in Terminal:

/home/maria        

And if I open up home.txt with the vim command:

vim home.txt        

The following would appear:

/home/maria        

If I were to combine the input and output operator, for example:

printenv 'HOME' < home.txt > home2.txt        

This command both redirects stdin and stdout, and also creates a new file called home2.txt if it does not already exist, thanks to the output operator (>).

When I run the following in Terminal:

cat home2.txt        

it returns the following in Terminal:

/home/maria        

When I run the following in Terminal:

cat home.txt        

it returns the following:

/home/maria        

However, because I am using the (single) regular output operator (>), it overwrites the previous content of home2.txt with a new instance of /home/maria. What if I didn't want to overwrite the contents of home2.txt?

The output redirector append operator (>>)

If I just want to append the contents of stdin (i.e., home.txt) to stdout (i.e., home2.txt), I would run the following in Terminal:

printenv 'HOME' < home.txt >> home2.txt        

Then, when I run the following command in Terminal:

cat home2.txt        

it returns the following in Terminal:

/home/maria
/home/maria        

And if I run the following in Terminal:

cat home.txt        

it returns the following in Terminal:

/home/maria        

The contents of home.txt remains the same because it is the stdin, but home2.txt now includes a second line of text, which has been appended to the original first line of text. No data was overwritten or lost.

An important thing to note: Usually one should use the output redirector append operator (>>) instead of the output redirector operator (>) so as not to lose any file data (unless there is a specific reason to use >)!

Related Resources

I/O redirection: by Machtelt Garrels, Introduction to Linux

Five ways to use redirect operators in Bash: by Damon Garn, RedHat


要查看或添加评论,请登录

Maria Campbell的更多文章

社区洞察

其他会员也浏览了