Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 245
Quinn Avatar asked Nov 30 '25 16:11

Quinn


1 Answers

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

like image 162
lw1.at Avatar answered Dec 02 '25 07:12

lw1.at



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!