Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I just noticed that it seems like the flag -e does not exist for the echo command in my shell on Linux. Is this just a messed up setting or is it "normal"?

Some code as an example:

#!/bin/sh
echo -e "\e[3;12r\e[3H"

Prints:

-e \e[3;12r\e[3H

This worked before! I guess some stty commands went terribly wrong and now it does not work anymore. Somebody suggested that my sh was actually just bash.

share|improve this question
    
There's no such thing as a "Linux shell". There's for instance the echo builtin of the /bin/sh shell as provided by the dash package on Debian (an operating system that can have Linux or FreeBSD as its kernel). –  Stéphane Chazelas Aug 27 '13 at 5:33
    
And this is why I just put escape characters in my script when I need them. –  Wumpus Q. Wumbley Aug 27 '13 at 14:59

3 Answers 3

up vote 9 down vote accepted

Because you used sh, not bash, then echo command in sh doesn't have option -e. From sh manpage:

echo [-n] args...
            Print the arguments on the standard output, separated by spaces.
            Unless the -n option is present, a newline is output following the
            arguments.

And it doesn't have \e, too:

        If any of the following sequences of characters is encountered
        during output, the sequence is not output.  Instead, the specified
        action is performed:

        \b      A backspace character is output.

        \c      Subsequent output is suppressed.  This is normally used at
                the end of the last argument to suppress the trailing new‐
                line that echo would otherwise output.

        \f      Output a form feed.

        \n      Output a newline character.

        \r      Output a carriage return.

        \t      Output a (horizontal) tab character.

        \v      Output a vertical tab.

        \0digits
                Output the character whose value is given by zero to three
                octal digits.  If there are zero digits, a nul character
                is output.

        \\      Output a backslash.

        All other backslash sequences elicit undefined behaviour.
share|improve this answer
2  
Several sh implementations support echo -e, at compilation time bash can be told not to support echo -e. It's just that that particular sh (probably dash) doesn't support -e while that particular bash does. –  Stéphane Chazelas Aug 27 '13 at 5:41

-e is not POSIX (in fact, POSIX echo generally accepts no options (though is allowed to support -n), see here), and /bin/sh on your system appears to be a POSIX shell. -e is an extension accepted in some shells, but you shouldn't rely on it, it's not portable. Ideally, use printf, or switch to using a shell which has echo -e.

Also see the caveats of \e in the comments below, which should be replaced with \033.

printf '\033[3;12r\033[3H'
share|improve this answer
1  
as gnouc points out, sh doesn't recognize \e either. Use \033 –  rici Aug 27 '13 at 3:15
    
@rici True, thanks. –  Chris Down Aug 28 '13 at 7:30

Note that at any time and in almost any shell, you can figure out which "echo" will be called by typing type echo or which echo. It's usually a shell builtin. So it depends on which "echo" is installed and on which shell you're using.

share|improve this answer
    
which echo should not be used, it likely won't tell you if you are using a builtin because which is usually an external binary. type is good, though. –  Chris Down Aug 27 '13 at 10:09
    
Well spotted, though on my main shell (zsh), it's a builtin (as revealed by type which or which which). ;) –  piojo Aug 27 '13 at 10:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.