简体   繁体   中英

access terminal emulator's colors in a bash script

In a bash script I need to print out some text in colors.

However, I don't want to specify fixed colors, I want to use the ones that are specified by the terminal(/-emulator).

How can I pro programmatically access them? Usually these must be a set of 16 colors. Two of them must be labeled background and foreground.

There should be a way to do so, since other programs as fish, emacs etc.. also appear in the colors specified in the terminal emulator.

Any advice is highly appreciated..

Make a color profile file profile like below

export RED='\033[0;31m'
export GREEN='\033[1;32m'
export WHITE='\033[1;37m'

Include the above file in your script and change the color using printf as needed.

#!/bin/bash
source $( dirname $0 )/profile

#Switching to RED

printf "${RED}"
ls

#Switching to GREEN

printf "${GREEN}"
echo "Current working directory is "`pwd`
echo "/etc/passwd contents"
cat /etc/passwd

#Switching back to white

printf "${WHITE}"

echo "Some white stuff"

The colors persist till we change them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM