How does one output bold text in Bash?

后端 未结 5 1831
南笙
南笙 2020-12-02 03:55

I\'m writing a Bash script that prints some text to the screen:

echo \"Some Text\"

Can I format the text? I would like to make it bold.

相关标签:
5条回答
  • 2020-12-02 04:09

    In theory like so:

    # BOLD
    $ echo -e "\033[1mThis is a BOLD line\033[0m"
    This is a BOLD line
    
    # Using tput
    tput bold 
    echo "This" #BOLD
    tput sgr0 #Reset text attributes to normal without clear.
    echo "This" #NORMAL
    
    # UNDERLINE
    $ echo -e "\033[4mThis is a underlined line.\033[0m"
    This is a underlined line. 
    

    But in practice it may be interpreted as "high intensity" color instead.

    (source: http://unstableme.blogspot.com/2008/01/ansi-escape-sequences-for-writing-text.html)

    0 讨论(0)
  • 2020-12-02 04:18

    The most compatible way of doing this is using tput to discover the right sequences to send to the terminal:

    bold=$(tput bold)
    normal=$(tput sgr0)
    

    then you can use the variables $bold and $normal to format things:

    echo "this is ${bold}bold${normal} but this isn't"
    

    gives

    this is bold but this isn't

    0 讨论(0)
  • 2020-12-02 04:20

    In order to apply a style on your string, you can use a command like:

    echo -e '\033[1mYOUR_STRING\033[0m'
    

    Explanation:

    • echo -e - The -e option means that escaped (backslashed) strings will be interpreted
    • \033 - escaped sequence represents beginning/ending of the style
    • lowercase m - indicates the end of the sequence
    • 1 - Bold attribute (see below for more)
    • [0m - resets all attributes, colors, formatting, etc.

    The possible integers are:

    • 0 - Normal Style
    • 1 - Bold
    • 2 - Dim
    • 3 - Italic
    • 4 - Underlined
    • 5 - Blinking
    • 7 - Reverse
    • 8 - Invisible
    0 讨论(0)
  • 2020-12-02 04:26

    This is an old post but regardless, you can also get boldface and italic characters by leveraging utf-32. There are even greek and math symbols that can be used as well as the roman alphabet.

    0 讨论(0)
  • 2020-12-02 04:27

    I assume bash is running on a vt100-compatible terminal in which the user did not explicitly turn off the support for formatting.

    First, turn on support for special characters in echo, using -e option. Later, use ansi escape sequence ESC[1m, like:

    echo -e "\033[1mSome Text"
    

    More on ansi escape sequences for example here: ascii-table.com/ansi-escape-sequences-vt-100.php

    0 讨论(0)
提交回复
热议问题