Using echo

A quick guide on how to use the echo command in the most practical way possible.

man is very direct in saying that the echo command is used to repeat arguments to std out. And that means passing practically anything to standard output. With echo we can use escape characters, such as tabulation, new line, activate text colors, background, character decoration, etc.

Escape

Without -e, it can’t interpret escaped commands.

$ echo "Oi\nEu sou o Echo."
Oi\nEu sou o Echo.

With -e, you enable the interpretation of escaped characters, such as \n to skip lines:

$ echo -e "Oi\nEu sou o Echo."
Oi
Eu sou o Echo.

For a new column , use the -e option with \v:

$ Nome="Echo"
$ echo -e "Oi\nSeu nome é $Nome \v$Nome \v$Nome"
Oi
Seu nome é Echo
                  Echo
                         Echo

To re-tab use the -e option with \t:

$ echo -e "\tOlá,\n\tEste é um texto tabulado."
    Olá,
    Este é um texto tabulado.

Avoid newline

If you don’t set the -n option, the information will be displayed on the screen, then a new line will appear:

$ echo "Oi, digite seu nome: "; read nome
Oi, digite seu nome: 
_

Now, with -n, we no longer have a newline after the sentence:

$ echo -n "Oi, digite seu nome: "; read nome
Oi, digite seu nome: _ 

This new line that echo creates can get in the way when it comes to getting the hexadecimal of a character

$ echo -e A |hexdump
0000000 41 0a <----- olha o newline aqui no 0a, atrapalhando
0000002

$ echo -en A |hexdump
0000000 41 <----- agora, só o hex 41, que é o A em ascii
0000001

Do you have any suggestions on how to use echo? Send it to me in the comments and I’ll insert it here =D

Comentários

More articles

How to use bc, the shell calculator

bc = bench calculator.
If you don’t know your shell’s calculator yet, it’s time to learn how to use it, even if its use is very basic.
The most trivial use of its functions should already cover most of your needs.
But don’t be fooled, this is a really powerful piece of software that should definitely be on your radar.

Read the article "

URL encoding/decoding with sed

There are various ways of encoding/decoding urls.
Programmers often use ready-made functions for this.
But do you really know what these functions are doing?
For this article, I’ve chosen sed as the tool to replace the codes and I point out the RFCs that discuss the subject.

Read the article "

Understand how to customize VIM on MacOS

If you’re already starting to get the hang of VIM, it’s time to take the next steps.
In this article, I’ll explain how to set up VIM for MacOS in what I consider to be the cleanest way (you may want to install it differently and that’s fine) and how to configure the NerdTree plugins, to access the directory tree; Status Tab to put some additional tools on the screen (and make VIM look very nice); and the Git plugin, to make version control easier without leaving the application.
Happy reading!

Read the article "

How to display colors in the terminal

Do you want to display texts with colors, bold, italics, underline, etc.?
Understanding a few rules and codes makes it easier than it sounds.
Learn how to display colors in your terminal with the clarity of someone who knows what they’re doing.

Read the article "
bureau-it.com