Read Command : Display the prompt in color (or enable interpretation of backslash escapes)

后端 未结 4 2024
星月不相逢
星月不相逢 2020-12-09 02:33

I often use something like read -e -p \"> All good ? (y/n)\" -n 1 confirm; to ask a confirm to the user.

I\'m looking for a way to colorize the outpu

4条回答
  •  失恋的感觉
    2020-12-09 02:55

    Break your query into two components:

    1. use echo -e -n to display the prompt
    2. collect the user response with read

    e.g:

    echo -e -n "\e[0;31mAll good (y/n)? "   # Display prompt in red
    echo -e -n '\e[0;0m'                    # Turn off coloured output
    read                                    # Collect the user input
    

    The echo -n option suppresses the trailing newline.

提交回复
热议问题