To color each new command in terminal, you've to edit ~/.bashrc file.
To do this,
gedit ~/.bashrc
Uncomment the line
# force_color_prompt=yes
to
force_color_prompt=yes
Then, You can restart the terminal or do
. ~/.bashrc
Original version
One way to do this is to get the parent process of your current shell session and from there the name of the terminal.
Get the parent of the current shell process. The bash variable $$
is the PID of your current shell, so we can give that as a query to ps
(-p $$
) and ask it tp print the PID of the parent process (-o ppid=
, the trailing =
is to avoid printing column headers):
$ ps -p $$ -o ppid=
544
So, the PID of my shell's parent is 544
.
Get the process associated with that PID and print its command line
$ ps -p 544 o args=
/usr/bin/python /usr/bin/terminator
The above output will depend on what terminal emulator you are using, I am using terminator
.
Combine everything in a single command
ps -p $(ps -p $$ -o ppid=) o args=
Use that to get the version
$(ps -p $(ps -p $$ -o ppid=) o args=) --version
terminator 0.97
Add a little function to your ~/.bashrc
that returns the name and version of the terminal emulator you're using (this works for most common terminal emulators):
which_term(){
term=$(ps -p $(ps -p $$ -o ppid=) -o args=);
found=0;
case $term in
*gnome-terminal*)
found=1
echo "gnome-terminal " $(dpkg -l gnome-terminal | awk '/^ii/{print $3}')
;;
*lxterminal*)
found=1
echo "lxterminal " $(dpkg -l lxterminal | awk '/^ii/{print $3}')
;;
rxvt*)
found=1
echo "rxvt " $(dpkg -l rxvt | awk '/^ii/{print $3}')
;;
## Try and guess for any others
*)
for v in '-version' '--version' '-V' '-v'
do
$term "$v" &>/dev/null && eval $term $v && found=1 && break
done
;;
esac
## If none of the version arguments worked, try and get the
## package version
[ $found -eq 0 ] && echo "$term " $(dpkg -l $term | awk '/^ii/{print $3}')
}
You can now get the name of the terminal and also pass any option you like to it (such as --version
.
Some examples using different terminals:
xterm
$ which_term
XTerm(297)
terminator
$ which_term
terminator 0.97
rxvt
, this one has none of the -V
, -version
or --version
flags so no version info is printed.
$ which_term
rxvt 1:2.7.10-5
gnome-terminal
.
$ which_term
gnome-terminal 3.10.1-1
konsole
$ which_term
Qt: 4.8.6
KDE Development Platform: 4.11.3
Konsole: 2.11.3
lxterminal
$ which_term
lxterminal 0.1.11-4
xfce4-terminal
$ which_term
xfce4-terminal 0.6.2 (Xfce 4.10)
Copyright (c) 2003-2012
The Xfce development team. All rights reserved.
Written by Benedikt Meurer <benny@xfce.org>
and Nick Schermer <nick@xfce.org>.
Please report bugs to <http://bugzilla.xfce.org/>.
New and improved
The above approach is not that trustworthy though. It will choke when you run your shell after su
ing to another user or when your terminal is aliased to something and various other cases. Since we are obviously working with X programs here, a better way might be to use something like xdotool
(installable with sudo apt-get install xdotool
) to get the information instead:
perl -lpe 's/\0/ /g' /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline
The above will print the command line used to launch the currently active window. Since your terminal will, presumably, be active, that is the command it will show. This means that for most terminal emulators, you can safely assume that the 1st field returned is the terminal name:
$ which_term
lxterminal
This means that getting the version is trivial. For example
$ dpkg -l $(which_term) | awk '/^ii/{print $3}'
0.1.11-4
Not so for gnome-terminal
:
$ which_term
/usr/lib/gnome-terminal/gnome-terminal-server
or terminator
:
$ which_term
/usr/bin/python /usr/bin/terminator
So, we can make it a little more complex (there are some bashisms here, this one is not portable):
which_term(){
term=$(perl -lpe 's/\0/ /g' \
/proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline)
## Enable extended globbing patterns
shopt -s extglob
case $term in
## If this terminal is a python or perl program,
## then the emulator's name is likely the second
## part of it
*/python*|*/perl* )
term=$(basename "$(readlink -f $(echo "$term" | cut -d ' ' -f 2))")
version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
;;
## The special case of gnome-terminal
*gnome-terminal-server* )
term="gnome-terminal"
;;
## For other cases, just take the 1st
## field of $term
* )
term=${term/% */}
;;
esac
version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
echo "$term $version"
}
This works for all cases I tested on.
Best Answer
When typing your command pipe it into "less" this allows you to scroll with the up/down arrows for example:
ls -l|less
Assuming you are talking about the standard bash terminal that is, which you probably are.