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.
bash shell-script colors ansi-term
add a comment |
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.
bash shell-script colors ansi-term
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
TryTERM=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
add a comment |
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.
bash shell-script colors ansi-term
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
bash shell-script colors ansi-term
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
TryTERM=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
add a comment |
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
TryTERM=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
add a comment |
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
.
1
Adding theexport COLOR=ANSI_CODE
to/etc/profile.d/ansi-colours.sh
and logging out and back in did the trick! Thanks!
– Fabby
2 days ago
add a comment |
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
.
1
Adding theexport COLOR=ANSI_CODE
to/etc/profile.d/ansi-colours.sh
and logging out and back in did the trick! Thanks!
– Fabby
2 days ago
add a comment |
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
.
1
Adding theexport COLOR=ANSI_CODE
to/etc/profile.d/ansi-colours.sh
and logging out and back in did the trick! Thanks!
– Fabby
2 days ago
add a comment |
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
.
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
.
edited 2 days ago
answered 2 days ago
Kusalananda
116k15218351
116k15218351
1
Adding theexport COLOR=ANSI_CODE
to/etc/profile.d/ansi-colours.sh
and logging out and back in did the trick! Thanks!
– Fabby
2 days ago
add a comment |
1
Adding theexport 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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