I have the following code fragment in my $HOME/.bashrc file:
export RED='\e[0;31m'
export GREEN='\e[0;32m'
export YELLOW='\e[0;33m'
export BLUE='\e[0;34m'
export MAGENTA='\e[0;35m'
export CYAN='\e[0;36m'
export WHITE='\e[0;37m'
#
export BOLD_RED='\e[1;31m'
export BOLD_GREEN='\e[1;32m'
export BOLD_YELLOW='\e[1;33m'
export BOLD_BLUE='\e[1;34m'
export BOLD_MAGENTA='\e[1;35m'
export BOLD_CYAN='\e[1;36m'
export BOLD_WHITE='\e[1;37m'
#
export BLACK='\e[\030'
export RESET_COLOR='\e[m'
export RESET_TERMINAL_COLOR="tput sgr0"
I want to modularize it as a function that is callable from all scripts.
I have removed the code from my $HOME/.bashrc file and made a file in my executable path called colorize-terminal containing
#!/bin/bash
function colorize-terminal {
[The code above goes in here]
}
and tried source colorize-terminal in a script to test it. But it does not give the expected colorization.
What am I doing wrong?
Thanks.
SOLUTION: based on the comments below and what I found out
I needed to rename both the function and the file
colorize-terminalto replace the hyphen by an underscore so:colorize_terminal. AFAICT, shell-script names are accepting of hyphens but this is not so for functions. They accept underscores instead.I needed to add
colorize_terminalas the last line in the file.After sourcing the function file, I tested it with
echo -e ${BOLD_YELLOW}"Hello there!"${RESET_COLOR}and it worked as expected.If I needed color upon login, I could source the file in
$HOME/.bashrc.
colorize-terminalto now be available in your current shell session? Finally, how does it fail? You say "it does not give the expected colorization", so what happens? Nothing? Different colors than you expected? Something else? Please edit your question and explain what you expect and what actually happens.sourceis a non-standard bash thing. The portable, standard command for sourcing a file is.sosourcewon't work if you're not using bash.function colorize-terminal {vscolorize-terminal() {colorize_terminal. (2) I included the function name as the last line of the file. Once these two were done, it works. Tested like this:echo -e ${BOLD_YELLOW}"Hello there!"${RESET_COLOR}. Since I am explicitly invokingbash, I prefersourceto.andfunction colorize_terminaltocolorize_terminal ().