echo ๋ช…๋ น์–ด๋กœ ์ค„๋ฐ”๊ฟˆ(๊ฐœํ–‰) ๋ฌธ์ž ์ž…๋ ฅ

์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž ์ฒ˜๋ฆฌ

๊ฐ€๋” shell script ์ž‘์„ฑํ• ๋•Œ echo ๋กœ new line ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค.


Linux ์— ํฌํ•จ๋œ echo ๋Š” backslash escape (\) ๋ฅผ ๋งŒ๋‚˜๋„ ํŠน๋ณ„ํ•˜๊ฒŒ ํ•ด์„ํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ \n ์„ ๋งŒ๋‚˜๋„ ๊ฐœํ–‰ ๋ฌธ์ž๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„์„œ ๋‹ค์Œ ๋ฌธ์žฅ์€ \n ๊ฐ€ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

$ echo "Hello\nWorld\n"


Hello\nWorld\n


echo๊ฐ€ ํƒˆ์ถœ ๋ฌธ์ž์ธ \ ๋ฅผ ๋งŒ๋‚˜๋ฉด ํŠน์ˆ˜ ๋ฌธ์ž๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•˜๋ ค๋ฉด -e ์˜ต์…˜์„ ์ฃผ๊ณ  ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

$ echo -e "Hello\nWorld\n"


Hello
World


echo manual ์˜ ํ•ด๋‹น ๋ถ€๋ถ„ ์„ค๋ช…์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

$ man echo
ย 
If -e is in effect, the following sequences are recognized:
       \\     backslash
       \n     new line
       \r     carriage return
       \t     horizontal tab


ํŠน์ˆ˜๋ฌธ์ž ์ถœ๋ ฅ

-e ์˜ต์…˜์„ ์ฃผ๊ณ  ์‹คํ–‰ํ•  ๊ฒฝ์šฐ \ ๋ฅผ ํŠน์ˆ˜ ๋ฌธ์ž๋กœ ์ฒ˜๋ฆฌํ•˜์ง€๋งŒ \! ๋‚˜ \? ๋Š” ํŠน์ˆ˜ํ•œ ์˜๋ฏธ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ˆ๋กœ ์•„๋ž˜์™€ย ๊ฐ™์€ .gitignore ๋ฅผ echo ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค.


์›ํ•˜๋Š” .gitignore
*
!.gitignore


-e ๋Š” ๋ฐฑ ์Šฌ๋ž˜์‹œ escape ๋ฅผ ์ธ์‹ํ•˜๋‚˜ ! ๋Š” bash ์˜ ํŠน์ˆ˜ ๋ฌธ์ž์ด๋ฏ€๋กœ ์˜๋„๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๊ณ  "event not found" ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

! ํŒŒ์‹ฑ ์—๋Ÿฌ
$ echo -e "*\n!.gitignore"     

-bash: !.gitignore": event not found


๊ทธ๋ž˜์„œ -e ์˜ต์…˜์„ ์ฃผ์–ด \ ๋ฅผ escape ๋กœ ํ•ด์„œ ์‹คํ–‰ํ•˜๋ฉด \! ๋Š” ํŠน์ˆ˜ํ•œ ์˜๋ฏธ๊ฐ€ ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ์˜๋„์™€๋Š” ๋‹ฌ๋ฆฌย ย \! ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

$ echo -e "*\n\!.gitignore"     

*
\!.gitignore

์ด๋Ÿด ๊ฒฝ์šฐ ! ๋ฌธ์ž์— ๋Œ€ํ•œ octal value(\0NNN ํ˜•์‹) ๋‚˜ hex (\xHH)๊ฐ’์„ ์ ์–ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


!์˜ octal(041) ๊ฐ’ ์‚ฌ์šฉ
$ echo -e "*\n\041.gitignore"

*
!.gitignore
!์˜ hex(21) ๊ฐ’ ์‚ฌ์šฉ
$ echo -e "*\n\x21.gitignore"

*
!.gitignore

๊ฐ™์ด ๋ณด๊ธฐ

Ref