How to change the output color of a command
You can color the output of any command using ANSI escape codes.
These are two examples:
echo -e "This is \033[31mred\033[0m"
d=`date`;echo -e "\033[31m$d\033[0m"
printf "\033[30;41mRed background, black text\033[0m"
- You can use
echo -e
orprintf
. - The pattern for adding color is:
\033[<FG ANSI code>;<BG ANSI code>m
. 31
is the ANSI code for foreground color red.0
resets the color.
Color name | Foreground color code | Background color code |
---|---|---|
black | 30 | 40 |
red | 31 | 41 |
green | 32 | 42 |
yellow | 33 | 43 |
blue | 34 | 44 |
magenta | 35 | 45 |
cyan | 36 | 46 |
white | 37 | 47 |
- You can find more color codes on Wikipedia.
- You can change the color of your terminal prompt by editing
PS1
environment variable (on your.bashrc
file for example).[ricardo@myarch Documents]$ echo $PS1 [\u@\h \W]\$
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: