What do <, << and <<< mean in Linux?
The <, << and <<< look similar to redirection indicators but they have a different purpose. Learn about them.
Don't they look like redirection indicators (> and >>)?
At first glance, even I got confused too! But
<<< has a completely different use case.
So in this tutorial, I'm going to walk you through how you can use them to level up your terminal game.
Meaning of <, << and <<< in Linux
Let's address their names and their purpose in brief
<(less than symbol): It is known as
Input Redirectionand is used to take input from a file rather than typing instructions manually.
<<: It is known as
Here Documentwhich is used to provide multiple lines of input directly to the command.
<<<: It is known as
Here Stringand used to provide a single line of input to the command.
I know it sounds super complex. But don't worry, I'm about to share some examples so you can have a better understanding of each.
1. The input redirection
Suppose that you have a script created to take some input from the user but there are times when the scripts are pretty huge and you want to automate the process.
Sure, you can customize the script itself but what if you can create a simple file that contains all the inputs for the script, and the whole execution can be automated?
That's one way of using input redirection!
And to use the input redirection, you'd have to follow a simple command syntax:
command < input.txt
Now, let's have a look at a simple example.
Here, I have a script named
And when I execute the script, it will ask either of the two inputs: yes or no:
But what if you want to skip the interactive process?
You can create a simple text file with the inputs you want to give to the script.
So I made a simple text file named
input.txt which had
yes written to it:
And when I used the input redirection over the script with the text file, it gave me the following result:
./input.sh < input.txt
2. The here document <<
The here document is used when one wants to give multiple lines of input to the command or script.
And to use it, you'd have to follow the simple syntax:
DELIMITER is used two times.
Once in the command which you saw above.
And in the end when you're done adding lines and want to save the changes. The DELIMITER can be any string of characters but I would recommend using
END as it represents the meaning much better!
Now, let's have a look at a simple example:
cat > output.txt <<END
Here, I have used the redirection between the cat command and the
output.txt text file which means the output of the cat command will be sent to the file.
Then, I used
<< with the
END delimiter which will let me write multiple lines and as soon as I type END, it will stop the command.
Here's a live example:
Pretty simple. Isn't it? The here document can also be used for adding multiline comments in bash scripts.
3. The here string <<<
The here string (triple less than signs) is used when one wants to input one line of stings to a script or a command.
To use the
<<<, you'd have to follow the given command syntax:
command <<< "string"
Let's have a look at a simple example.
You must be familiar with the wc command which is used to count words, characters, and more from the file.
But what if you want to count words or characters from a single line? You can use the here string in that case:
wc -w <<< "Demo text from sagar sharma!"
Pretty neat. Isn't it?
Learn about the redirection of the data stream
The redirection of input can be done using > and >> which looks close to what I explained above. And for that purpose, we made a detailed guide:
I hope you will find this guide helpful.