How to make a text blink in shell script

前端 未结 4 1773
孤街浪徒
孤街浪徒 2021-02-02 10:57

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

4条回答
  •  滥情空心
    2021-02-02 12:02

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

提交回复
热议问题