Sponsored Content
Full Discussion: Using color in scripts
Operating Systems Linux Ubuntu Using color in scripts Post 303033725 by Don Cragun on Wednesday 10th of April 2019 05:47:13 PM
Old 04-10-2019
Expanding a little on what wisecracker has already said....

It looks like you are trying to use ANSI terminal escape codes, but they start with an <escape> character; not with the character <3>. Furthermore, you haven't told us what shell you're using and the behavior of echo -e varies considerably from shell to shell, from operating system to operating system, and even if you use the same shell on multiple systems, the behavior of echo -e may vary even when echo is a built-in utility within a given shell.

Using printf avoids the various behaviors of echo and the changes to your color introducing variables can be fixed by replacing the leading 3 with \033 (i.e., the ASCII escape character). In bash you can replace the \033 with \e. In ksh93 you can replace the \033 with either \e or \E. These uses of \e and \E can be used to reference the <escape> character even when you're using those shells on a mainframe that uses EBCDIC instead of an ASCII based codeset. (But you probably won't find terminals that accept ANSI terminal escape codes on a mainframe.)

Note also that it is usually considered to be bad form to write these escape sequences into a file unless the file is associated with a terminal device. For example, on systems where you can tell the ls utility to use various colors to indicate various types of files, it only does that when standard output is directed to a terminal device file. If you redirect the output of ls to a regular file, it won't output the escape sequences to produce those colors.

Code:
GREEN='\033[0;32m'
RED='\033[0;31m'
WHITE='\033[0;37m'
RESET='\033[0m'
FILE="/usr/share/sounds/My_Sounds/Short_doorbell.wav"

if [ -f "$FILE" ]
then
     printf "${GREEN}File exists.${RESET}\n"
else
     printf "${RED}File does NOT exist.${RESET}\n"  
fi

For other ANSI terminal escapes (such as bold, underlined, and/or crossed out text and cursor motion and blink rates see ANSI escapes codes in Wikipedia.
These 3 Users Gave Thanks to Don Cragun For This Post:
 

2 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to change the background color in the init 3 mode(not line color)

Hello, I am using RHEL 6.1 on VMware I am searching for a way to change background color (not line by line color wich one can using tput command) basically changing the color of the whole screen to white instead of the default black and changing font color to black and alos would like to... (2 Replies)
Discussion started by: Dexobox
2 Replies

2. UNIX for Beginners Questions & Answers

Text color in Linux scripts via putty

hi Folks, Can anyone help with changing the color of the words in a linux shell script? I get how to change default background etc in putty, but for some reason the text in the script has different colors for different parts of the cript. Is there a way to have one color in a linux shell... (5 Replies)
Discussion started by: jonnyd
5 Replies
All times are GMT -4. The time now is 04:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy