1

So I have the following variables defined in /etc/bash.bashrc:

RS="\033[0m"    # reset
HC="\033[1m"    # hicolor
UL="\033[4m"    # underline
INV="\033[7m"   # inverse background and foreground
FBLK="\033[30m" # foreground black
FRED="\033[31m" # foreground red
FGRN="\033[32m" # foreground green
FYEL="\033[33m" # foreground yellow

And when I do an echo -e "$FRED Red" at the prompt, I actually get Red in red on gnome-terminal, but when I execute:

#!/bin/bash

echo -e "$FRED Red"
echo -e "$FYEL Yellow"
echo -e "$FGRN Green"

I get everything in the default colour even though $TERM is xterm-256color.

screenshot including exact output

What am I doing wrong?

Note: Eventually I want to echo Errors in red, Warnings in yellow and Info in green in my scripts.

4
  • That is dependent on your terminal/$TERM. I suspected it and tested in in fluxbox/lxterminal and it shows colours in both cases. Depending on ssh confs/software/plataform your TERM can and will change. Would you add $TERM contents on both cases? Nov 17 '18 at 12:25
  • edited @RuiFRibeiro
    – Fabby
    Nov 17 '18 at 12:34
  • Try TERM=xterm at the top of the script just for testing it out. Nov 17 '18 at 12:37
  • Not a Mac. New Screenshot
    – Fabby
    Nov 17 '18 at 12:41
2

The variables in your /etc/bash.bashrc file are not exported. The file is read by any interactive non-login shell, but not by shell scripts (these are non-interactive).

Since the variables are not exported, they are not available in the environment of your script.

I would suggest not modifying the distribution-provided file /etc/bash.bashrc and instead:

  • add the variables in the script itself (where they don't need to be exported), or
  • add them (and export them) in your personal .bashrc file, or
  • add them (and export them) in a separate .sh file under /etc/profile.d which would export the variables for any login shell. A login shell is started either by your terminal application, or by your graphical environment when you log in (or both).

If you add the variables anywhere other than in the script itself (in a file not explicitly sourced by the script), then the variables would not be available if you run the script from cron.

1
  • 2
    Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
    – Fabby
    Nov 17 '18 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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