color in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting color in bash
# 1  
Old 04-01-2012
color in bash

I have some tcsh scripts that produce output in color, but does not work in bash. Any idea on a solution?

Code:
  echo "  \033[2;32m"
  echo "  Optional Arguments
  echo -n "\033[0m"

# 2  
Old 04-01-2012
echo behavior varies with shell, whereas printf behavior is consistent.
Code:
printf "  \033[2;32m"
printf "  Optional Arguments"
printf "\033[0m"

for your echo script following will work, but I would recommend to use printf version
Code:
echo -e "  \033[2;32m"
echo -e "  Optional Arguments"
echo -e "\033[0m"

# 3  
Old 04-01-2012
Ahhh. I understand. I take your advice.

---------- Post updated at 06:25 PM ---------- Previous update was at 06:17 PM ----------

what is the equivalent printf to
Code:
echo -n

where the code does not skip a line after output.

Last edited by kristinu; 04-01-2012 at 08:41 PM..
# 4  
Old 04-01-2012
printf does not output the trailing newline, by default.

So if you do this at command line:
Code:
printf "\033[2;32mHello\033[0m"

you would see hello on console in green and the prompt right next to it.
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 04-01-2012
A full featured color script!
Code:
#!/bin/bash

[[ x${1// /} == x || ${#1} > 3 ]] && T=gYw || T=${1}

esc="\e"
reset="${esc}[0m"

echo
echo -n "            "
echo -n 4{0..7}m | sed 's# #    #g' 
echo -n "   "
echo -n 1\;4{0..7}m | sed 's# #  #g'
echo -n "   0m    "
echo {1..8}m | sed 's# #   #g'

for FG in 3{0..7} 1\;3{0..7}; do
     for((i=0; i<5-${#FG}; ++i)); do
           echo -n " "
     done
     echo -en "${FG}${esc}[${FG}m ${T} "
     i=0
     for BG in 4{0..7} 1\;4{0..7} {0..8}; do
         (( i > 16 )) && {
         echo -en "${esc}[${FG};${BG}m ${T} ${reset}"
         } || echo -en "${esc}[${FG};${BG}m  ${T}  ${reset}"
         ((++i))
     done
     echo
done
echo


Last edited by complex.invoke; 04-01-2012 at 11:11 PM..
# 6  
Old 04-01-2012
Ok,
I have added some color to my prompt, but I never tried to color my command outputs. I have tried the examples above on my CentOS and Mac computers and there was no color add to the out put. What happens is the above echo statements changes my prompt and the printf puts the out put in front of my prtompt. What am I missing here?
# 7  
Old 04-01-2012
Quote:
Originally Posted by bitlord
Ok,
I have added some color to my prompt, but I never tried to color my command outputs. I have tried the examples above on my CentOS and Mac computers and there was no color add to the out put. What happens is the above echo statements changes my prompt and the printf puts the out put in front of my prtompt. What am I missing here?
To have you prompt showed in red, just replace the line below in /etc/bashrc to the later one:
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\e[1;31m[\u@\h \W]\\$\e[0m "
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - changing a color of a substring

Hello! I need to write a bash script for my university classes, and I came up with an idea of a program that would test the speed of typing - there is some random text that you have to rewrite, and the script measures time, number of mistakes etc. The text would be visible on the screen all... (3 Replies)
Discussion started by: xqwzts
3 Replies

2. OS X (Apple)

Adding Color to bash

Hey everyone, I have come across an issue to where I am trying to create a script which changes the text color with a simple if then statement. I have seen it done with Fedora 8 but when I try and create it using my MacBook Pro running Snow Leopard it doesn't work. Funny thing is, when I use... (2 Replies)
Discussion started by: dachadster13
2 Replies

3. Shell Programming and Scripting

bash: color strings in log file

hello im looking for an easy way to color specific strings in text file. simple example: cat file1 acb 1234 qwer 5678 my goal: cat file1 (color the acb red and the 5678 blue) acb 1234 qwer 5678 cheers (2 Replies)
Discussion started by: modcan
2 Replies

4. UNIX for Dummies Questions & Answers

How to get rid of all the weird characters and color on bash shell

Does anyone of you know how to turn off color and weird characters on bash shell when using the command "script"? Everytime users on my server used that command to record their script, they either couldn't print it because lp kept giving the "unknown format character" messages or the print paper... (1 Reply)
Discussion started by: Micz
1 Replies
Login or Register to Ask a Question