Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo outputs -e parameter in bash scripts. How can I prevent this?

Tags:

bash

echo

I've read the man pages on echo, and it tells me that the -e parameter will allow an escaped character, such as an escaped n for newline, to have its special meaning. When I type the command

$ echo -e 'foo\nbar' 

into an interactive bash shell, I get the expected output:

foo bar 

But when I use this same command (i've tried this command character for character as a test case) I get the following output:

-e foo  bar 

It's as if echo is interpretting the -e as a parameter (because the newline still shows up) yet also it interprets the -e as a string to echo. What's going on here? How can I prevent the -e showing up?

like image 720
Sam Svenbjorgchristiensensen Avatar asked Dec 14 '10 04:12

Sam Svenbjorgchristiensensen


People also ask

How do I turn off output in bash?

To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.

How do you prevent Ctrl C cause the termination of a running bash script?

How do I disable Control-C? You need to use the trap command which can enable or disable keyboard keys such as Crtl-C. SIGINT is Crtl-C and it is represented using number 2. Signal names are case insensitive and the SIG prefix is optional.

What is echo bash script?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.

How do I stop a bash script execution?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.


1 Answers

You need to use #!/bin/bash as the first line in your script. If you don't, or if you use #!/bin/sh, the script will be run by the Bourne shell and its echo doesn't recognize the -e option. In general, it is recommended that all new scripts use printf instead of echo if portability is important.

In Ubuntu, sh is provided by a symlink to /bin/dash.

like image 51
Dennis Williamson Avatar answered Sep 23 '22 11:09

Dennis Williamson