Shell Script - echo語法的用法

參考資料

參考FLOZz’ MISC的說明頁面

基本語法

1
echo -? "you can see this"

echo的後面可以加-e, -n , -p

最常見的是使用-e因為加上-e後可以讓""內的顏色控制碼生效,換行碼生效等等效果

內文使用

可以使用

  • \n - 換行
  • \e - 跟字型有關的控制碼開頭

Formatting

Set
CodeDescriptionExamplePreview
1Bold/Brightecho -e "Normal \e[1mBold"Normal BoldNormal Bold
2Dimecho -e "Normal \e[2mDim"Normal DimNormal Dim
4Underlinedecho -e "Normal \e[4mUnderlined"Normal UnderlinedNormal Underlined
5Blink 1)echo -e "Normal \e[5mBlink"Normal BlinkNormal Blink
7Reverse (invert the foreground and background colors)echo -e "Normal \e[7minverted"Normal invertedNormal inverted
8Hidden (useful for passwords)echo -e "Normal \e[8mHidden"Normal HiddenNormal Hidden
Reset
CodeDescriptionExamplePreview
0Reset all attributesecho -e "\e[0mNormal Text"Normal TextNormal Text
21Reset bold/brightecho -e "Normal \e[1mBold \e[21mNormal"Normal Bold NormalNormal Bold Normal
22Reset dimecho -e "Normal \e[2mDim \e[22mNormal"Normal Dim NormalNormal Dim Normal
24Reset underlinedecho -e "Normal \e[4mUnderlined \e[24mNormal"Normal Underlined NormalNormal Underlined Normal
25Reset blinkecho -e "Normal \e[5mBlink \e[25mNormal"Normal Blink NormalNormal Blink Normal
27Reset reverseecho -e "Normal \e[7minverted \e[27mNormal"Normal inverted NormalNormal inverted Normal
28Reset hiddenecho -e "Normal \e[8mHidden \e[28mNormal"Normal Hidden NormalNormal Hidden Normal

8/16 colors

Foreground (text)
CodeColorExamplePreview
39Default foreground colorecho -e "Default \e[39mDefault"Default DefaultDefault Default
30Blackecho -e "Default \e[30mBlack"Default BlackDefault Black
31Redecho -e "Default \e[31mRed"Default RedDefault Red
32Greenecho -e "Default \e[32mGreen"Default GreenDefault Green
33Yellowecho -e "Default \e[33mYellow"Default YellowDefault Yellow
34Blueecho -e "Default \e[34mBlue"Default BlueDefault Blue
35Magentaecho -e "Default \e[35mMagenta"Default MagentaDefault Magenta
36Cyanecho -e "Default \e[36mCyan"Default CyanDefault Cyan
37Light grayecho -e "Default \e[37mLight gray"Default Light grayDefault Light gray
90Dark grayecho -e "Default \e[90mDark gray"Default Dark grayDefault Dark gray
91Light redecho -e "Default \e[91mLight red"Default Light redDefault Light red
92Light greenecho -e "Default \e[92mLight green"Default Light greenDefault Light green
93Light yellowecho -e "Default \e[93mLight yellow"Default Light yellowDefault Light yellow
94Light blueecho -e "Default \e[94mLight blue"Default Light blueDefault Light blue
95Light magentaecho -e "Default \e[95mLight magenta"Default Light magentaDefault Light magenta
96Light cyanecho -e "Default \e[96mLight cyan"Default Light cyanDefault Light cyan
97Whiteecho -e "Default \e[97mWhite"Default WhiteDefault White
Background
CodeColorExamplePreview
49Default background colorecho -e "Default \e[49mDefault"Default DefaultDefault Default
40Blackecho -e "Default \e[40mBlack"Default BlackDefault Black
41Redecho -e "Default \e[41mRed"Default RedDefault Red
42Greenecho -e "Default \e[42mGreen"Default GreenDefault Green
43Yellowecho -e "Default \e[43mYellow"Default YellowDefault Yellow
44Blueecho -e "Default \e[44mBlue"Default BlueDefault Blue
45Magentaecho -e "Default \e[45mMagenta"Default MagentaDefault Magenta
46Cyanecho -e "Default \e[46mCyan"Default CyanDefault Cyan
47Light grayecho -e "Default \e[47mLight gray"Default Light grayDefault Light gray
100Dark grayecho -e "Default \e[100mDark gray"Default Dark grayDefault Dark gray
101Light redecho -e "Default \e[101mLight red"Default Light redDefault Light red
102Light greenecho -e "Default \e[102mLight green"Default Light greenDefault Light green
103Light yellowecho -e "Default \e[103mLight yellow"Default Light yellowDefault Light yellow
104Light blueecho -e "Default \e[104mLight blue"Default Light blueDefault Light blue
105Light magentaecho -e "Default \e[105mLight magenta"Default Light magentaDefault Light magenta
106Light cyanecho -e "Default \e[106mLight cyan"Default Light cyanDefault Light cyan
107Whiteecho -e "Default \e[107mWhite"Default WhiteDefault White