I saw someone can make the output in bash shell to display colorfully. Not only highlight the error or warning, but also the directory when execute 'ls'.
Does anyone can tell me how to make it? Thanks a lot.
I saw someone can make the output in bash shell to display colorfully. Not only highlight the error or warning, but also the directory when execute 'ls'.
Does anyone can tell me how to make it? Thanks a lot.
You can check out this and this:-
Use ANSI escape sequences to set text properties like foreground and background colors.
EXAMPLE:-
echo -e "\e[1;34mThis is a blue text.\e[0m"
and
#!/bin/bash
# tputcolors
echo
echo -e "$(tput bold) reg bld und tput-command-colors$(tput sgr0)"
for i in $(seq 1 7); do
echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) \$(tput setaf $i)"
done
echo ' Bold $(tput bold)'
echo ' Underline $(tput sgr 0 1)'
echo ' Reset $(tput sgr0)'
echo
These things were around since the days of DOS. Use ANSI Escape Sequences or Codes:
http://wiki.bash-hackers.org/scripting/terminalcodes
Example usage:
This would print ABC in green text:
ESCAPE=$'\e'
echo "${ESCAPE}[0;32mABC${ESCAPE}[0m"
You could also use -e option:
echo -e "\e[0;32mABC\e[0m"
Or printf:
printf "\e[0;32mABC\e[0m\n"
Check out some of the codes I mainly use in scripts here.
In the specific case of ls
:
If you use a linux distro, almost all of which come with gnu ls
, you probably already have coloured output. If you don't, try ls -C
or ls --color=auto
. You may need to set $LS_COLORS
; see man dircolors
for a way to do it easily.
But probably you are on Mac OS X, which uses a different version of ls
. If your terminal outputs colors, you can probably get colored ls
output with ls -G
, but again you may need to set $LSCOLORS
. Also, you may need to export CLICOLOR=1
in order to get the terminal to show colours. For more information, see this answer on a companion site.
Here's a little bit of background, if you are interested.
As others have pointed out, the terminals respond to what are called escape sequences. These are sequence of characters that you can send to the terminal (write, display), and instead of displaying these characters, the terminal hardware interprets them as commands to do various things. These things could be anything from moving the cursor, to blinking, to changing the foreground and background colors. This is what ANY terminal program does (be it vi, emacs, ls, or bash) when it needs to invoke certain terminal capabilities.
The problem is that there are many different kinds of terminals, that have varying capabilities, and respond to different escape sequences. Early on, people came up with a mechanism to deal with this. They came up with termcap, and then terminfo, which is a way to abstract the terminal capabilities. So, you do things like "move cursor", or "set foreground color", and the underlying library retrieves the correct escape sequence from a DB maintained for different terminals (based on the value of TERM environment variable, etc.).
Thus you have the famous curses library. It lets you do all this. For things like bash, they have encapsulated this functionality in a command you can invoke, called tput. The man page give you the details of how to invoke it. So, it would be preferable to not use escape sequences directly. But rather you should go through tput so that you remain independent of the terminal you happen to be working with.