33

I have this piece of code below:

echo "\033[33mTitle of the Program\033[0m"

which changes the colour to yellow.

How can I make the text "Title of the Program" blink?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74

6 Answers6

49

Try adding -e if it's not working without it

You may need to add the -e option to echo (at least that's required on all or most systems I use). The following will tell your system to blink the text:

echo  -e "\033[5mTitle of the Program\033[0m"

You can have blinking and color

And you don't have to choose either yellow or blinking. You can have your cake and eat it too:

echo  -e "\033[33;5mTitle of the Program\033[0m"

Some systems ignore blink codes

Your system may ignore the blink code—this appears to be quite common. If you want to make the text prominent, but blinking is being ignored, you can instead invert the colors with 7:

echo  -e "\033[33;7mTitle of the Program\033[0m"

Or you can use blinking and inversion of colors (and yellow):

echo  -e "\033[33;5;7mTitle of the Program\033[0m"
iconoclast
  • 21,213
  • 15
  • 102
  • 138
19

Whether you can make blink work or not depends upon the terminal emulator. The system itself is irrelevant.

The example given in the question was close, needing only a change to the escape sequence to "work" with any POSIX shell:

echo  "\033[33;5mTitle of the Program\033[0m"

The -e suggested is unneeded (it is a bashism, non-standard, and usually unneeded). Changing the 7 (reverse) to 5 (blink) does what was asked.

Rather than hardcoding the escape, you could use tput, e.g.,

printf '%s%s%s%s' "$(tput setaf 3)" "$(tput blink)" "Title of the Program" "$(tput sgr0)"

for the same effect, with two differences:

  • the expression is arguably more readable,
  • actually uses the known terminal capabilities, but
  • assumes you are using a suitable terminal description, i.e., $TERM.

For example VTE (the library used for various skins such as gnome-terminal) does not support blink (and it's possible to find its developers' opinions on the topic in various bug reports). Using infocmp to display the corresponding terminal capabilities shows that, plus some other differences:

$ infocmp vte xterm
comparing vte to xterm.
    comparing booleans.
        km: F:T.
        mc5i: F:T.
        npc: F:T.
    comparing numbers.
    comparing strings.
        blink: NULL, '\E[5m'.
        cnorm: '\E[?25h', '\E[?12l\E[?25h'.
        cvvis: NULL, '\E[?12;25h'.
        enacs: '\E)0', NULL.
        is2: '\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8', '\E[!p\E[?3;4l\E[4l\E>'.
        kb2: '\E[E', '\EOE'.
        kfnd: '\E[1~', NULL.
        kslt: '\E[4~', NULL.
        mc0: NULL, '\E[i'.
        mc4: NULL, '\E[4i'.
        mc5: NULL, '\E[5i'.
        rep: NULL, '%p1%c\E[%p2%{1}%-%db'.
        rmacs: '^O', '\E(B'.
        rmcup: '\E[2J\E[?47l\E8', '\E[?1049l'.
        rmm: NULL, '\E[?1034l'.
        rs2: '\E7\E[r\E8\E[m\E[?7h\E[!p\E[?1;3;4;6l\E[4l\E>\E[?1000l\E[?25h', '\E[!p\E[?3;4l\E[4l\E>'.
        setb: NULL, '\E[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m'.
        setf: NULL, '\E[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m'.
        sgr: '\E[0%?%p6%t;1%;%?%p2%t;4%;%?%p5%t;2%;%?%p7%t;8%;%?%p1%p3%|%t;7%;m%?%p9%t\016%e\017%;', '%?%p9%t\E(0%e\E(B%;\E[0%?%p6%t;1%;%?%p5%t;2%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m'.
        sgr0: '\E[0m\017', '\E(B\E[m'.
        smacs: '^N', '\E(0'.
        smcup: '\E7\E[?47h', '\E[?1049h'.
        smm: NULL, '\E[?1034h'.

If you happen to use KDE konsole, the differences are longer (although konsole happens to support blink).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
5

Just use printf instead of echo:

printf "\e[6;33mTitle of the Program\e[0m"

This line prints Title of the Program with yellow, blinking text.

Attila
  • 3,206
  • 2
  • 31
  • 44
3

A small work around to make the line blink in bash script

for (( i=0;i<=3;i++))
do
#Below line will deleted the before printed line
echo -en "\033[1A"
echo -en "EmpNo:$empno already exists\n";
sleep 0.4s;
#Below line to print a blank line
echo -en "\033[1A"
echo -en "                                                            \n";                                                                         
sleep 0.2s;
done
echo -en "\033[2A"
echo -en "                                                            \n";
echo -en "\033[1A"
echo -en "Enter the empno       : "; read empno1;
Nike
  • 33
  • 7
1

As far as I can tell, it's meant to be:

echo " \033[5mTitle of the Program\033[0m"

The only change is before the title, the 33m is replaced by 5m. See Ansi Escape Codes

Note that some systems filter out blink, because it's really annoying. Your mileage may vary.

AMADANON Inc.
  • 5,753
  • 21
  • 31
  • What does `tput blink` produce for you? You might want to convert to hex by piping to `od`, `hexdump`, `xxd`, or a similar tool: `tput blink | xxd` – tripleee Jul 03 '13 at 04:30
  • I just ran tput blink, and it produced the equivalent (and didn't work). Most terminals don't support blink, because it's annoying. – AMADANON Inc. Jul 03 '13 at 04:33
  • 2
    "Many people (myself included) object strongly to the "blink" attribute. Fortunately, it doesn't work in any terminal emulators that I'm aware of." -- http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-6.html – tripleee Jul 03 '13 at 04:33
  • 1
    Just throwing this out there but the IBM 3161 terminal next to me *does* support blinking. I just spent about half an hour figuring out why irssi's default theme was making blinking text everywhere. (if anyone finds this through google, the solution is this theme https://github.com/asenchi/irssi-theme-doc/blob/master/colorless.theme ) – Wyatt Ward Aug 02 '15 at 16:38
  • http://misc.flogisoft.com/bash/tip_colors_and_formatting#terminals_compatibility ... konsole and xterm support blinking according to this list. I tested konsole and it works. – sjas Oct 26 '15 at 09:46
  • blink doesn't work on VTE, You have to use `xterm` instead of ANSI or `tput` – PersianGulf Mar 16 '16 at 23:35
  • @PersianGulf - that's what I meant when I said "Note that some systems filter out blink, because it's really annoying. Your mileage may vary." – AMADANON Inc. Mar 17 '16 at 01:15
1
  1. export variables
export YELLOWIT="\e[1;3;5;43m"
export NC="\e[0m"
  1. use in echo to get desired effects
echo -e "\n${YELLOWIT}### RESTORE FROM USB - Firefox Bookmarks ###${NC}\n"
buddemat
  • 4,552
  • 14
  • 29
  • 49