I'm trying to add colors to this bash script, can anyone help?

#!/bin/bash
# Script to connect to the sprint network

# Colors
# ESC_SEQ=“\x1b[“
COL_RESET=$ESC_SEQ“39;49;00m“
COL_RED=$ESC_SEQ“31;01m“
COL_GREEN=$ESC_SEQ“32;01m“
COL_YELLOW=$ESC_SEQ“33;01m“
COL_BLUE=$ESC_SEQ“34;01m“
COL_MAGENTA=$ESC_SEQ“35;01m“
COL_CYAN=$ESC_SEQ“36;01m“

case "$1" in

connect)
	pppd call sprint updetach
	echo -e $COL_GREEN"**You are now connected to the sprint network!"$COL_RESET
	;;

disconnect)
	killall pppd
	echo -e $COL_RED"**You have disconnected from the sprint network!"$COL_RESET
	;;

esac

Here are the errors:

root@mybox:~/Scripts# ./sprint disconnect
./sprint: line 6: 49: command not found
./sprint: line 6: 00m“: command not found
./sprint: line 7: 01m“: command not found
./sprint: line 8: 01m“: command not found
./sprint: line 9: 01m“: command not found
./sprint: line 10: 01m“: command not found
./sprint: line 11: 01m“: command not found
./sprint: line 12: 01m“: command not found
“31**You have disconnected from the sprint network!“39

Dani AI

Generated

Diagnosis: the error messages are classic signs of non-ASCII/incorrect quoting. Curly (typographic) quotes or stray characters in the ESC_SEQ lines turn the escape bytes into literal text, so the shell tries to execute fragments like 49 or prints "x1b[". Echo variants (echo -e) are also inconsistent across shells. For a reliable, bash-only solution use the $'...' quoting form to produce real escape bytes and printf to print them.

# bash-only: set color variables to actual escape bytes
GREEN=$'\e[32;1m'
RED=$'\e[31;1m'
RESET=$'\e[0m'

# print with printf (avoid echo -e portability issues)
printf '%b\n' "${GREEN}**You are now connected to the sprint network!${RESET}"
printf '%b\n' "${RED}**You have disconnected from the sprint network!${RESET}"

Troubleshooting checklist:

  • Look for curly quotes and hidden bytes with sed -n l script, cat -A script or xxd script. Replace any “ or ” with straight quotes (") and remove stray characters.
  • Convert Windows line endings if present: dos2unix script or tr -d '\r' < script > fixed.
  • Confirm the shebang is #!/bin/bash (the $'...' form is a bash feature).
  • To debug expansions and see what the shell runs, use set -x or bash -x script.
  • If terminal-aware colors are wanted, capture the terminal-reset or color codes from the terminal-control utility into variables using command substitution (the same principle applies).

As noted, switching away from the broken ESC_SEQ fixed the output; 's numeric-escape idea points in the same direction. The $'...' + printf approach keeps color values in variables, avoids echo portability headaches, and prevents the smart-quote / tokenization errors that produced the original messages.

Recommended Answers

All 4 Replies

Sorry actually this is the error:

root@mybox:~/Scripts# ./sprint disconnect
./sprint: line 6: 49: command not found
./sprint: line 6: 00m“: command not found
./sprint: line 7: 01m“: command not found
./sprint: line 8: 01m“: command not found
./sprint: line 9: 01m“: command not found
./sprint: line 10: 01m“: command not found
./sprint: line 11: 01m“: command not found
./sprint: line 12: 01m“: command not found
“x1b[““31**You have disconnected from the sprint network!“x1b[““39

I uncommented

# ESC_SEQ=“\x1b[“

I was just doing that for testing...

This is working....but not exactly the way i'd like for variables:

#!/bin/bash
# Script to connect to the sprint network

# Colors
#ESC_SEQ='\x1b['
#COL_RESET='; tput sgr0'
#COL_RESET=$ESC_SEQ“39;49;00m“
COL_RED='\E[31;01m'
COL_GREEN='\E[32;01m'
COL_YELLOW='\E[33;01m'
COL_BLUE='\E[34;01m'
COL_MAGENTA='\E[35;01'
COL_CYAN='\E[36;01m'


case "$1" in

connect)
	pppd call sprint updetach
	echo -e "$COL_GREEN**You are now connected to the sprint network!" ; tput sgr0
	;;

disconnect)
	killall pppd
	echo -e "$COL_RED**You have disconnected from the sprint network!" ; tput sgr0
	;;

esac

Hey there,

It may just be an issue with calling the variables.

If you change

COL_GREEN='\E[32;01m'

to

COL_GREEN="\033[32;1m"

it might work better with the "echo -e" line.

Hope that helps :)

, Mike

Sorry I haven't tried it. What I changed is working fine now...I was just trying to replace the "tput sgr0" with a variable...unless someone knows how to do that. I couldn't get it working right...probably was going wrong with the single and double quotes.

If no one knows then I'll mark this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.