0

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

  1. I needed to rename both the function and the file colorize-terminal to 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.

  2. I needed to add colorize_terminal as the last line in the file.

  3. After sourcing the function file, I tested it with echo -e ${BOLD_YELLOW}"Hello there!"${RESET_COLOR} and it worked as expected.

  4. If I needed color upon login, I could source the file in $HOME/.bashrc.

6
  • Don't put it in a function, just cut/paste the code as-is into your new file. If you are going to put it in a function for some reason then add the call to that function within the same file. Commented Mar 8, 2023 at 18:46
  • What is the expected behavior when sourcing? Are you expecting that to actually change the colors, or are you expecting the new function colorize-terminal to 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. Commented Mar 8, 2023 at 18:49
  • Oh, and remember that source is a non-standard bash thing. The portable, standard command for sourcing a file is . so source won't work if you're not using bash. Commented Mar 8, 2023 at 18:50
  • .... ditto for function colorize-terminal { vs colorize-terminal() { Commented Mar 8, 2023 at 18:51
  • There were two errors: (1) No hyphens are allowed in the function name. So, I renamed the file and function to 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 invoking bash, I prefer source to . and function colorize_terminal to colorize_terminal (). Commented Mar 9, 2023 at 2:16

1 Answer 1

0

As suggested by @Kusalananda, here is my solution.

  1. As explained here a function name can only consist of letters, numerals, and the underscore character. The hyphen is not permitted. So, colorize-terminal needed to be renamed as both a file and a function to colorize_terminal.

The bash-specific working code is:

#!/bin/bash
function colorize_terminal {
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 BOLD='\e[\033'
export RESET_COLOR='\e[m'
export RESET_TERMINAL_COLOR="tput sgr0"
}

Put that code into a file named colorize_terminal. Invoke it from within any script by specifying the path to the file so:

source /path-to-colorize-terminal/colorize_terminal.

Its definitions will be available to the script.

It may be tested with a simple one-line command like:

echo -e "${BOLD_YELLOW}""Hello World!""${RESET_COLOR}"

Ideally, one could have this functionality at startup by including a line like

source "$HOME/bin/colorize_terminal"

in the file "$HOME/.bashrc.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.