How to insert a line break inside the echo shell script?

Asked

Viewed 47,253 times

15

I was wondering if there’s any way to insert a line break inside the echo of the shell script.

For example, in the command:

echo "Bom dia fulano"

I’d like the exit to be:

Bom dia
fulano

I know you can do it like this:

echo "bom dia"
echo "fulano"

But I’d really like to know if there’s any way to do that with a echo only.

  • There are several other options besides -e. Take a look at this short course: http://rberaldo.com.br/curso-shell-script-comandos-basicos-linux/#cmd_echo

4 answers

16


Try

echo -e "Bom dia\nfulano"

The option e makes the character of newline be interpreted.

  • 1

    It worked, thank you!!!

  • 1

    There are several other options besides -e. Take a look at this short course: http://rberaldo.com.br/curso-shell-script-comandos-basicos-linux/#cmd_echo

  • 1

    @Beraldo other options for line breaking? And that on your page I only see printf(...) as a candidate to replace the echo -e

  • 1

    There is also the expansion of $"Bomdia nMundo" and $'Bomdia nMundo', without even asking echo interpret escapes; these expansions are from Shell itself and indicate to interpret the escapes inside the quotes

2

You can use in printf that works well:

printf "Ola\nMundo\n"

1

You can do it that way too:

echo $'Bom dia\nfulano'

How this expansion is done by bash and not by command echo, the argument $'Bom dia\nfulano' works with any command, and not only with the echo or the printf. Example:

$ figlet $'Bom dia\nfulano'
 ____                        _ _       
| __ )  ___  _ __ ___     __| (_) __ _ 
|  _ \ / _ \| '_ ` _ \   / _` | |/ _` |
| |_) | (_) | | | | | | | (_| | | (_| |
|____/ \___/|_| |_| |_|  \__,_|_|\__,_|

  __       _                   
 / _|_   _| | __ _ _ __   ___  
| |_| | | | |/ _` | '_ \ / _ \ 
|  _| |_| | | (_| | | | | (_) |
|_|  \__,_|_|\__,_|_| |_|\___/ 

-1

this format also works:

echo "
Bom dia

Fulano!"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.