Bash: Colored Output with a Variable
bash, variables
Solution
I've slightly changed your code:
#!/bin/bash
function pause() {
prompt="$1"
echo -e -n "\033[1;36m$prompt"
echo -e -n '\033[0m'
read
clear
}
pause "Press enter to continue..."
What I've changed:
- You were initializing prompt to $3, when the correct argument was $1
- The ANSI sequence was incorrect. See: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
- The call to read was incorrect, you were passing several arguments do to the use of $*. In this particular case you are discarding the input, so it's not even necessary to save the result of read. I suggest you to read the manpage: http://linux.die.net/man/1/bash to see how to exactly use read. If you pass in several arguments, those arguments will be mapped to variable names that will contain the different fields inputted in the line.
Problem
I have the following function: ``` function pause #for prompted pause until ENTER { prompt="$3" echo -e -n "\E[36m$3" #color output text cyan echo -e -n '\E[0m' #ends colored output read -p "$*" #read keys from user until ENTER. clear } pause "Press enter to continue..." ``` However, my function refuses to apply the cyan color to the string I pass into the function. A similar question was asked here, but it seems that I'm doing everything correctly...