Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a text blink in shell script

Tags:

bash

blink

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"

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


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;

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.