That’s right, in today’s article we’re going to learn a little about the ‘echo’ command. We’ll barely be scratching the surface of the ‘echo’ command. There are so many ways to use ‘echo’ that we simply can’t fit it all into an article. Do, read on!
So, what is ‘echo’? Well, basically, the ‘echo’ command is a command that tells the computer to output text – be it in the terminal or using standard output to add that text to a file. It’s useful for regular people, but you’ll find it’s most commonly used in things like bash scripting
That sort of thing, scripting, is outside the scope of this article – and probably beyond the scope of the entire site. Maybe I’ll get to it, but those would end up being long articles, or articles written in parts. Besides, I’m not really all that good with scripting. (If you are good with scripting and would like to write some articles, do let me know.)
This is how the man page describes the ‘echo’ command:
echo – display a line of text
That’s indeed what it does. When you combine it with things like variables, you open up a whole new world. I don’t actually have a very good article about variables, other than existing variables. I should probably have said article.
Anyhow, we’ll just be scratching the surface of the ‘echo’ command. There are all sorts of ways to use ‘echo’, but we’re just going to use it in ways that a new Linux user might use it. Some basic familiarity is probably a good thing.
The ‘echo’ Command:
This article requires an open terminal. If you don’t know how to open the terminal, you can do so with your keyboard – just press
So, how you start with this ‘echo’ command:
1 | echo I love Linux-Tips.us |
You can also add quotes, and in some cases probably should:
1 | echo "I love Linux-Tips.us" |
You can also use the > operand and echo the text to a file. Like so:
1 | echo "I love Linux-Tips.us" > new_file.txt |
Read: How To: Write Text To A File From The Terminal with “>” and “>>”
However, that’s not all you can do with the ‘echo’ command. You can also automatically fill in the information by way of environment variables. For example, try this:
1 | echo $SHELL |
Or you can try:
1 | echo $XDG_SESSION_DESKTOP |
Read: How To: Show All Environment Variables
See? It’s good that I’ve written so many articles. I get to refer to ’em and save us all a bunch of time. So, be sure to visit those articles – especially if you’re a new user. If you’ve forgotten something, you can ‘echo’ the variable to be reminded.
Bonus:
This next bit is really just some information you’re gonna need down the road. I can sense it! This is a bit advanced for some of you, and you won’t need this until later in your Linux journey, but others will see this and suddenly understand the potential. If you’re new, you’ll get there.
The first thing we’re going to do is set a variable. It’s actually very easy to set your own variables in Linux. In this case, we’ll use:
1 | x=Linux-Tips |
Now, let’s just ‘echo’ the variable you just created:
1 | echo $x |
Or, even better, you can then try:
1 | echo "I love $x" |
See? It will output the variable when you echo it. You can create all the variables you’ll realistically ever need.
You can actually format the output from the ‘echo’ command. For example, the -e
flag with the \n
separator. An example command looks like:
1 | echo -e "I \nlove \nLinux-Tips.us" |
You can format the text in a variety of ways, just read the man page. You can do quite a lot with the ‘echo’ command and you should definitely check the man page if you’re interested in exploring all the possibilities:
1 | man echo |
There’s a ton of ways to use ‘echo’ (and with it variables) for those with a clever mind. When you do move on to learn about scripting, you’ll learn that it’s a basic tool used for all sorts of things, in all sorts of clever ways.
Closure:
And there you have it. You have a new article. This time, we’ve covered just the slightest bit of the ‘echo’ command. It’s enough to get your mind working, or so I hope. It’s enough to get a slight taste of the potential and to understand why you may want to be familiar with it.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.