Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert 'echo' to 'echo -e' to enable color escape codes #44

Closed
wants to merge 1 commit into from
Closed

Convert 'echo' to 'echo -e' to enable color escape codes #44

wants to merge 1 commit into from

Conversation

bstrie
Copy link

@bstrie bstrie commented Dec 8, 2011

On my machine, using this tool looks like the following:

[user@machine ~]$ n
  \033[32mο\033[0m 0.6.5 \033[90m\033[0m

To make the color escape codes work properly, just had to add the '-e' flag to the echoes that use them.

Not sure how this wasn't noticed before now, but I suppose it's
possible that some shells come with a version of 'echo' where the
'-e' flag is considered active by default. However, on my system
(CentOS 6.0, bash 4.1.2) doing any sort of command that echoes color
codes prints garbage like the following:

    [user@machine ~]$ n
      \033[32mο\033[0m 0.6.5 \033[90m\033[0m

All this commit does is add the '-e' flag to only those 'echo'
commands that are using color escape codes. This causes the above
command to print its output as expected.

I'm not sure if this will break anything on shells that assume '-e'
implicitly.

For reference, here is the section of the 'echo' man page regarding
'-e':

   -e     enable interpretation of backslash escapes

       If -e is in effect, the following sequences are recognized:

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \e     escape

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \0NNN  byte with octal value NNN (1 to 3 digits)

       \xHH   byte with hexadecimal value HH (1 to 2 digits)
@bstrie
Copy link
Author

bstrie commented Dec 8, 2011

...Of course, why would I ever bother looking at the issues to check to see if someone's already submitted a fix :P

In any case, is it confirmed that adding -e to the echo calls even breaks anything? I've seen other projects that don't bother special-casing their echoes. If this isn't the case, then adding an option for color as per #18 seems like overkill.

@tj
Copy link
Owner

tj commented Dec 8, 2011

I get:

-e   ο 0.4.12 
-e     0.6.4

@bstrie
Copy link
Author

bstrie commented Dec 8, 2011

I see. :(

Out of curiosity, what's your setup?

@tj
Copy link
Owner

tj commented Dec 8, 2011

osx bash 3.2.48

@tj
Copy link
Owner

tj commented Dec 8, 2011

upgraded to 4.2.x, same deal

@bstrie
Copy link
Author

bstrie commented Dec 8, 2011

Here, try this command instead of echo:

printf "\e[1;34mThis should be blue.\n\e[0m"

If this works for you, then I think we can assume that printf can be swapped in for echo.

@tj
Copy link
Owner

tj commented Dec 8, 2011

i'll give it a try, i've had less interoperability issues with printf (oddly), bit of a piss-off that such as simple command like echo is varied haha

@tj tj closed this in 8d1ec71 Dec 8, 2011
@tj
Copy link
Owner

tj commented Dec 8, 2011

seems fine for me

@disfated
Copy link

getting

  \e[32mο\e[0m 0.6.6 \e[90m\e[0m

with printf on ubuntu bash 4.2.10

replacing \e with \033 fixes this for me.

@tj
Copy link
Owner

tj commented Dec 16, 2011

damnit haha, i dont get why everyone ditches \e it's so handy

@bstrie
Copy link
Author

bstrie commented Dec 16, 2011

Might I suggest following the example given here by including this at the top of the script:

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

Supposedly tput should be able to determine a valid representation of each color code for any given terminal. You then use it like this:

printf "${RED}red text ${NORMAL}normal text\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants