LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-20-2012, 12:09 PM   #1
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Rep: Reputation: 18
Strange characters in bash script....


Ugh! I am getting these strange characters when I run my bash script.

I am using colors that I use in my PS1 prompt but when I run them in a bash script to produce colored output I get these strange characters.

Code:
    # Reset colors
    clr_off='\033[00m'       # Text Reset

    # Regular colors
    rd='\[\033[00;31m'          # Red
    blck='\[\033[00;30m'        # Black
    grn='\[\033[00;32m'        # Green
    yllw='\[\033[00;33m'       # Yellow
    bl='\[\033[00;34m'         # Blue
    prpl='\[\033[00;35m'       # Purple
    cyn='\[\033[00;36m'         # Cyan
    wht='\[\033[00;37m'        # White
Here is the code:

Code:
#!/bin/bash
host_name=$(cat /proc/sys/kernel/hostname)
cpu_vendor=$(awk '/vendor_id/ {print $3}' /proc/cpuinfo | head -n 1)

echo; echo -e "System Report for: $rd$host_name$clr_off on $(date -R)"; echo

echo -e "Processor:\t\t\t$bl$cpu_vendor$clr_off"
The output:

Code:
System Report for: \[debian6 on Sun, 20 May 2012 13:06:43 -0400

Processor:			\[GenuineIntel
Notice the:

Code:
\[
How do I get rid of those two characters from the script???
 
Old 05-20-2012, 12:43 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,821

Rep: Reputation: Disabled
I believe the correct escape sequence is "\E", not "\[":
Code:
# Reset colors
clr_off='\033[00m'       # Text Reset

# Regular colors
rd='\E\033[00;31m'         # Red
blck='\E\033[00;30m'       # Black
grn='\E\033[00;32m'        # Green
yllw='\E\033[00;33m'       # Yellow
bl='\E\033[00;34m'         # Blue
prpl='\E\033[00;35m'       # Purple
cyn='\E\033[00;36m'        # Cyan
wht='\E\033[00;37m'        # White
 
Old 05-20-2012, 12:55 PM   #3
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
not working

Ser Olmy: That did not work on by Debian box.
 
Old 05-20-2012, 01:11 PM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,821

Rep: Reputation: Disabled
I'm don't think it's related to your distribution as such, but it could be related to your terminal settings. Did the code I posted get rid of the [-characters, but failed to provide colour? Does your terminal support colours? What does echo $TERM produce?

This script works on my system. I'm using PuTTY to connect from a Windows system, and my $TERM variable says 'xterm'.
Code:
#!/bin/bash

clr_off='\033[00m'

rd='\033[00;31m'
blck='\033[00;30m'
grn='\033[00;32m'
yllw='\033[00;33m'
bl='\033[00;34m'
prpl='\033[00;35m'
cyn='\033[00;36m'
wht='\033[00;37m'

echo -en "${rd}Red, ${grn}green, ${yllw}yellow, ${bl}blue, "
echo -e "${prpl}purple, ${cyn}cyan, ${wht}white.${clr_off}"

host_name=$(cat /proc/sys/kernel/hostname)
cpu_vendor=$(awk '/vendor_id/ {print $3}' /proc/cpuinfo | head -n 1)

echo -e "\nSystem Report for:\t${rd}${host_name}${clr_off} on $(date -R)"
echo -e "Processor:\t\t${bl}${cpu_vendor}${clr_off}\n"
...and as colucix points out in post #5 and #8 below, '\033' is ASCII code 27 (escape), which is the same as \e. In other words, I originally had two escape codes in my script, which was totally unnecessary but by chance still produced the correct colours.

Last edited by Ser Olmy; 05-20-2012 at 01:44 PM. Reason: Typo in code
 
Old 05-20-2012, 01:17 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Just remove the extra \[ at the beginning of each color definition. \e (escaped lowercase e) is an alias for the escape character which is octal \033. Indeed, these are called ANSI Escape Sequences. Hence the sequence:
Code:
\033[0;31m
is the same as
Code:
\e[0;31m
For a nice explanation about colors in bash, see this ArchWiki page.



Edit: according to the Advanced Bash Scripting Guide:
Quote:
Use the following template for writing colored text on a colored background.

echo -e '\E[COLOR1;COLOR2mSome text goes here.'

The "\E[" begins the escape sequence. The semicolon-separated numbers "COLOR1" and "COLOR2" specify a foreground and a background color
the \E (uppercase) is allowed as well, but putting it before \033 is an unused repetition and brings to an extra control character in the output (you can easily verify using od -c).

Last edited by colucix; 05-20-2012 at 01:23 PM. Reason: Added more information.
 
1 members found this post helpful.
Old 05-20-2012, 01:19 PM   #6
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
solution

echo $TERM is:

xterm

This is what worked.

Code:
rd='\033[00;31m'
I removed the \[ from the variable.

This works in the PS1 prompt:

Code:
rd='\[\033[00;31m'
but not in my scripts.

This means that I need seperate color variabes for my PS1 prompt and seperate color variables for scripts, I guess. There must be a better way...
 
Old 05-20-2012, 01:23 PM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,821

Rep: Reputation: Disabled
Does the script at the bottom of this page work on your system?
 
Old 05-20-2012, 01:24 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Quote:
Originally Posted by cryingthug View Post
This works in the PS1 prompt:

Code:
rd='\[\033[00;31m'
but not in my scripts.
How your prompt looks like? The escaped bracket should appear as a literal bracket.

Maybe you missed my reply above in post #5.

Last edited by colucix; 05-20-2012 at 01:26 PM.
 
Old 05-20-2012, 01:38 PM   #9
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
Yes. That does work on my system.

Quote:
Originally Posted by Ser Olmy View Post
Does the script at the bottom of this page work on your system?
 
Old 05-20-2012, 02:01 PM   #10
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,821

Rep: Reputation: Disabled
Quote:
Originally Posted by cryingthug View Post
Yes. That does work on my system.
OK, then I think you have a solution.

Also, see my revised script in post #4. I was thrown off track by the '\[' sequence, beliving it was intended to be the start of an escape sequence. Seems that it was just a superfluous character sequence, and omitting it entirely should produce the intended results.
 
Old 05-20-2012, 02:02 PM   #11
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
my prompt

colucix:

This is what my prompt looks like:

Code:
\[\033[38;05;240m\][\h@\u] \w \$ \033[00m

Quote:
Originally Posted by colucix View Post
How your prompt looks like? The escaped bracket should appear as a literal bracket.

Maybe you missed my reply above in post #5.
 
Old 05-20-2012, 02:15 PM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Code:
\[\033[38;05;240m\][\h@\u] \w \$ \033[00m
The two escaped brackets highlighted in red are meant to begin and terminate a sequence of non-printing characters respectively. They are not really necessary and I cannot tell you in which cases they become mandatory. To be sure, try to set
Code:
PS1='\033[38;05;240m[\h@\u] \w \$ \033[00m'
and see if there is any difference in the appearance of your prompt. Said that, you can safely use the same color sequences both for your prompt and in your scripts. A complete list of allowed escape sequences (other than colors) in bash prompt, here.
 
Old 05-20-2012, 02:19 PM   #13
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,821

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Code:
\[\033[38;05;240m\][\h@\u] \w \$ \033[00m
The two escaped brackets highlighted in red are meant to begin and terminate a sequence of non-printing characters respectively. They are not really necessary and I cannot tell you in which cases they become mandatory.
I think I just found out: Remove the brackets, and then try to cycle through your command history with the arrow keys.

But you can still use the exact same colour sequences, just put '\[' and '\]' around them when defining PS1.
 
1 members found this post helpful.
Old 05-20-2012, 02:28 PM   #14
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Quote:
Originally Posted by Ser Olmy View Post
I think I just found out: Remove the brackets, and then try to cycle through your command history with the arrow keys.

But you can still use the exact same colour sequences, just put '\[' and '\]' around them when defining PS1.
Good shot! Cycling through the command history is a mess. I cannot even delete the string using backspace, since it sticks when encounter the escape character.
 
Old 05-20-2012, 02:29 PM   #15
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
colucix and ser olmy: I will give your suggestions a try. Thank you for all your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
non-ascii characters in bash script and unicode igor.R Linux - Newbie 31 12-29-2012 03:45 AM
bash script to remove first characters from every line (00) Linux - General 8 08-01-2011 10:28 AM
Bash Script to get the first characters of a string onesikgypo Programming 12 11-14-2008 09:57 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
inserting special characters into mysql with bash script ihopeto Linux - Newbie 1 12-05-2006 12:46 PM


All times are GMT -5. The time now is 01:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration