'How to make a text blink in shell script

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?



Solution 1:[1]

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"

Solution 2:[2]

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).

Solution 3:[3]

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.

Solution 4:[4]

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;

Solution 5:[5]

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.

Solution 6:[6]

  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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Attila
Solution 4 Nike
Solution 5 AMADANON Inc.
Solution 6 buddemat