Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zsh trouble when using echo with color/formatting characters

Tags:

alias

zsh

I'm just switch to zsh and now adapting the alias in which was printing some text (in color) along with a command.

I have been trying to use the $fg array var, but there is a side effect, all the command is printed before being executed.
The same occur if i'm just testing a echo with a color code in the terminal:

echo $fg_bold[blue] "test"
]2;echo "test"  test     #the test is in the right color

Why the command print itself before to do what it's supposed to do ? (I precise this doesn't happen when just printing whithout any wariable command) Have I to set a specific option to zsh, use echo with a special parameter to get ride of that?

like image 665
AdrieanKhisbe Avatar asked May 15 '13 10:05

AdrieanKhisbe


2 Answers

Execute the command first (keep its output somewhere), and then issue echo. The easiest way I can think of doing that would be:

echo $fg[red] `ls`

Edit: Ok, so your trouble is some trash before the actual output of echo. You have some funny configuration that is causing you trouble.

What to do (other than inspecting your configuration):

  • start a shell with zsh -f (it will skip any configuration), and then re-try the echo command: autoload colors; colors; echo $fg_bold[red] foo (this should show you that the problem is in your configuration).
  • Most likely your configuration defines a precmd function that gets executed before every command (which is failing in some way). Try which precmd. If that is not defined, try echo $precmd_functions (precmd_functions is an array of functions that get executed before every command). Knowing which is the code being executed would help you search for it in your configuration (which I assume you just took from someone else).

If I had to guess, I'd say you are using oh-my-zsh without knowing exactly what you turned on (which is an endless source of troubles like this).

like image 133
Francisco Avatar answered Oct 17 '22 19:10

Francisco


I don't replicate your issue, which I think indicates that it's either an option (that I've set), or it's a zsh version issue:

$ echo $fg_bold[red] test
 test

Because I can't replicate it, I'm sure there's an option to stop it happening for you. I do not know what that option is (I'm using heavily modified oh-my-zsh, and still haven't finished learning what all the zsh options do or are).

My suggestions:

You could try using print:

$ print $fg_bold[red] test
 test

The print builtin has many more options than echo (see man zshbuiltins).

You should also:

  • Check what version zsh you're using.
  • Check what options (setopt) are enabled.
  • Check your ~/.zshrc (and other loaded files) to see what, if any, options and functions are being run.

This question may suggest checking what TERM you're using, but reading your question it sounds like you're only seeing this behaviour (echoing of the command after entry) when you're using aliases...?

like image 45
simont Avatar answered Oct 17 '22 18:10

simont