Quinn
Quinn

Reputation: 9496

zsh colours only work when command used in terminal directly

I'm trying to write a zsh script which can echo some useful information with colours. As a test I made the following file:

#!/bin/zsh

echo $fg[red] "test"

which should just output test in red, however when I run it it outputs test but it is in the default text colour rather then red.

If I just type echo $fg[red] "test" directly into my terminal, then it works correctly and I see the red test, but when I run my file with the above code, the colours don't work.

Is there something I'm missing or doing wrong?

Upvotes: 3

Views: 503

Answers (1)

lw1.at
lw1.at

Reputation: 1577

You need to load colors first that is shipped with zsh (/usr/share/zsh/functions/Misc/colors for me) and initialized all the $fg, $bg and more variables

#!/bin/zsh
autoload colors && colors
echo $fg[red] "test"

will print a red test

Upvotes: 3

Related Questions