Colour Errors / Warnings / Information in bash script











up vote
1
down vote

favorite












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



RS="33[0m"    # reset
HC="33[1m" # hicolor
UL="33[4m" # underline
INV="33[7m" # inverse background and foreground
FBLK="33[30m" # foreground black
FRED="33[31m" # foreground red
FGRN="33[32m" # foreground green
FYEL="33[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.










share|improve this question
























  • 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?
    – Rui F Ribeiro
    2 days ago










  • edited @RuiFRibeiro
    – Fabby
    2 days ago










  • Try TERM=xterm at the top of the script just for testing it out.
    – Rui F Ribeiro
    2 days ago












  • Not a Mac. New Screenshot
    – Fabby
    2 days ago















up vote
1
down vote

favorite












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



RS="33[0m"    # reset
HC="33[1m" # hicolor
UL="33[4m" # underline
INV="33[7m" # inverse background and foreground
FBLK="33[30m" # foreground black
FRED="33[31m" # foreground red
FGRN="33[32m" # foreground green
FYEL="33[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.










share|improve this question
























  • 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?
    – Rui F Ribeiro
    2 days ago










  • edited @RuiFRibeiro
    – Fabby
    2 days ago










  • Try TERM=xterm at the top of the script just for testing it out.
    – Rui F Ribeiro
    2 days ago












  • Not a Mac. New Screenshot
    – Fabby
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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



RS="33[0m"    # reset
HC="33[1m" # hicolor
UL="33[4m" # underline
INV="33[7m" # inverse background and foreground
FBLK="33[30m" # foreground black
FRED="33[31m" # foreground red
FGRN="33[32m" # foreground green
FYEL="33[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.










share|improve this question















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



RS="33[0m"    # reset
HC="33[1m" # hicolor
UL="33[4m" # underline
INV="33[7m" # inverse background and foreground
FBLK="33[30m" # foreground black
FRED="33[31m" # foreground red
FGRN="33[32m" # foreground green
FYEL="33[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.







bash shell-script colors ansi-term






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked 2 days ago









Fabby

2,94411125




2,94411125












  • 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?
    – Rui F Ribeiro
    2 days ago










  • edited @RuiFRibeiro
    – Fabby
    2 days ago










  • Try TERM=xterm at the top of the script just for testing it out.
    – Rui F Ribeiro
    2 days ago












  • Not a Mac. New Screenshot
    – Fabby
    2 days ago


















  • 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?
    – Rui F Ribeiro
    2 days ago










  • edited @RuiFRibeiro
    – Fabby
    2 days ago










  • Try TERM=xterm at the top of the script just for testing it out.
    – Rui F Ribeiro
    2 days ago












  • Not a Mac. New Screenshot
    – Fabby
    2 days ago
















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?
– Rui F Ribeiro
2 days ago




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?
– Rui F Ribeiro
2 days ago












edited @RuiFRibeiro
– Fabby
2 days ago




edited @RuiFRibeiro
– Fabby
2 days ago












Try TERM=xterm at the top of the script just for testing it out.
– Rui F Ribeiro
2 days ago






Try TERM=xterm at the top of the script just for testing it out.
– Rui F Ribeiro
2 days ago














Not a Mac. New Screenshot
– Fabby
2 days ago




Not a Mac. New Screenshot
– Fabby
2 days ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










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.






share|improve this answer



















  • 1




    Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
    – Fabby
    2 days ago













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482327%2fcolour-errors-warnings-information-in-bash-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










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.






share|improve this answer



















  • 1




    Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
    – Fabby
    2 days ago

















up vote
3
down vote



accepted










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.






share|improve this answer



















  • 1




    Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
    – Fabby
    2 days ago















up vote
3
down vote



accepted







up vote
3
down vote



accepted






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Kusalananda

116k15218351




116k15218351








  • 1




    Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
    – Fabby
    2 days ago
















  • 1




    Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
    – Fabby
    2 days ago










1




1




Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
– Fabby
2 days ago






Adding the export COLOR=ANSI_CODE to /etc/profile.d/ansi-colours.sh and logging out and back in did the trick! Thanks!
– Fabby
2 days ago




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482327%2fcolour-errors-warnings-information-in-bash-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio