๊ฐ์ด ๋ณด๋ฉด ์ข์ ๊ธ
[Linux/Unix] 03-4. sh ํ์ผ / ๋งค๊ฐ๋ณ์ ํ์ฅ ๋ฌธ๋ฒ / ${:-}, ${:=}, ${:+}, ${:?}
๊ฐ์ด ๋ณด๋ฉด ์ข์ ๊ธ2025.10.21 - [5. Computer Science Basics ๐งฑ/Linux & Unix ๐ง] - [Linux/Unix] 03-1. bash metacharacter (*, ?, $, &)/echo ๋ช ๋ น์ด/Bash C printf() ๋ฌธ๋ฒ [Linux/Unix] 03-1. bash metacharacter (*, ?, $, &)/echo ๋ช ๋ น์ด/Bash C pri
lxvxxu.tistory.com
[Linux/Unix] 03-1. bash metacharacter (*, ?, $, &)/echo ๋ช ๋ น์ด/Bash C printf() ๋ฌธ๋ฒ
๐งญ Bash ๋ฉํ๋ฌธ์ (Metacharacter)๋ฉํ๋ฌธ์(Metacharacter)๋ Bash์์ ํน๋ณํ ์๋ฏธ๋ฅผ ๊ฐ๋ ๊ธฐํธ๋ฅผ ๋งํ๋ค.ํ์ผ๋ช ํ์ฅ(globbing), ๋ช ๋ น ์คํ, ๋ณ์ ์นํ, ๋ฐฑ๊ทธ๋ผ์ด๋ ์ฒ๋ฆฌ ๋ฑ ๋ค์ํ ๊ธฐ๋ฅ์ ์ํํ๋ค.๐ 1) * (
lxvxxu.tistory.com
#!/bin/bash
echo number of arguments is $#
echo arguments are $*
echo command is $0
echo first argument is $1
shift
echo number of arguments is $#
echo arguments are $*
echo command is $0
echo first argument is $1
exit 0
๋ฌด์์ ํ๋ ์คํฌ๋ฆฝํธ์ธ๊ฐ?
์ด ์คํฌ๋ฆฝํธ๋ ๋ช
๋ นํ ์ธ์(positional parameters) ๊ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง ๋ณด์ฌ์ฃผ๋ ์์ ์
๋๋ค.
ํนํ shift๊ฐ $1, $2, … ๋ฅผ ํ ์นธ์ฉ ์ผ์ชฝ์ผ๋ก ์ด๋์ํค๋ ํจ๊ณผ๋ฅผ ๋ฐ๋ชจํฉ๋๋ค.
ํต์ฌ ๊ธฐํธ ์ค๋ช
- $# : ํ์ฌ ์ธ์์ ๊ฐ์
- $* : ๋ชจ๋ ์ธ์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ์ฐ๊ฒฐํ ๋ฌธ์์ด
- $0 : ์คํ๋ ์คํฌ๋ฆฝํธ์ ์ด๋ฆ(๊ฒฝ๋ก)
- $1 : ์ฒซ ๋ฒ์งธ ์ธ์
- shift [n] : ์ธ์๋ฅผ n์นธ ์ผ์ชฝ์ผ๋ก ์ด๋(๊ธฐ๋ณธ 1์นธ).
- ์คํ ํ $1 ← ๊ธฐ์กด $2, $2 ← ๊ธฐ์กด $3 …
- ์ธ์ ๊ฐ์ #$๋ ์ค์ด๋ฆ
์ค-by-์ค ๋์
1. ์ด๊ธฐ ์ํ ์ถ๋ ฅ
echo number of arguments is $#
echo arguments are $*
echo command is $0
echo first argument is $1
- ์ธ์์ ๊ฐ์, ์ ์ฒด ์ธ์, ์คํฌ๋ฆฝํธ ์ด๋ฆ, ์ฒซ ๋ฒ์งธ ์ธ์๋ฅผ ๋ณด์ฌ์ค๋๋ค.
shift
- ๋ชจ๋ ์ธ์๋ฅผ ํ ์นธ ์ผ์ชฝ์ผ๋ก ์ด๋์ํต๋๋ค.
์: ์๋ $1=a, $2=b, $3=c ์๋ค๋ฉด → shift ํ $1=b, $2=c ๊ฐ ๋จ.
#$๋ 1 ๊ฐ์
echo number of arguments is $#
echo arguments are $*
echo command is $0
echo first argument is $1
- ์ด๋ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํฉ๋๋ค.
exit 0
์คํ ์์ 1: ์ธ์ 3๊ฐ ์ ๋ฌ
$ bash demo.sh alpha beta gamma
์ถ๋ ฅ
number of arguments is 3
arguments are alpha beta gamma
command is demo.sh
first argument is alpha
number of arguments is 2
arguments are beta gamma
command is demo.sh
first argument is beta
์ค๋ช :
- ์ฒ์์ #$=3, $1=alpha.
- shift ํ #$=2, $1์ด beta๋ก ๋ฐ๋.
- $0(์คํฌ๋ฆฝํธ ์ด๋ฆ)์ ๋ณํ์ง ์์.
์คํ ์์ 2: ์ธ์ 1๊ฐ ์ ๋ฌ
$ bash demo.sh only
์ถ๋ ฅ
number of arguments is 1
arguments are only
command is demo.sh
first argument is only
number of arguments is 0
arguments are
command is demo.sh
first argument is
์ค๋ช :
- shift ํ์ ๋ ์ด์ ์ธ์๊ฐ ์์ด #$=0, $1์ ๋น ๊ฐ.
ํ · ์ฃผ์์ฌํญ
- shift n์ฒ๋ผ ์ซ์๋ฅผ ์ฃผ๋ฉด n์นธ ์ด๋ํฉ๋๋ค.
์: shift 2 → $1←$3, $2←$4 … - ๋จ์ ์ธ์ ์๋ณด๋ค ํฐ ์๋ก shiftํ๋ฉด ์๋ฌ:
shift 5
# bash: shift: shift count out of range
- ์ธ์๋ฅผ ๊ทธ๋๋ก ๋ณด์กดํ๋ ค๋ฉด $*๋ณด๋ค "$@"๊ฐ ๋ ์์ ํฉ๋๋ค(๊ณต๋ฐฑ ํฌํจ ์ธ์๋ ๋ถ๋ฆฌ ์ ์ง).
- ์ด ์คํฌ๋ฆฝํธ๋ ๋ฐ๋ชจ ๋ชฉ์ ์ด๋ผ $*๋ฅผ ์ฌ์ฉํ์ง๋ง, ์ค๋ฌด์์ ๋ณดํต "$@"๋ฅผ ๊ถ์ฅํฉ๋๋ค.
ํ ์ค ์์ฝ
์ด ์คํฌ๋ฆฝํธ๋ $#, $*, $0, $1์ ์ถ๋ ฅํด๋ณด๊ณ , shift๋ก ์ธ์๋ฅผ ํ ์นธ ๋ฐ์ด ์ธ์ ๊ฐ์์ ์ฒซ ์ธ์๊ฐ ์ด๋ป๊ฒ ๋ณํ๋์ง ํ์ธํ๋ ์์ ์ ๋๋ค.


'6. CS ๊ธฐ์ด ๐ > Linux & Unix ๐ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Linux/Unix][4-2] inode์ ์ ๋ณด (0) | 2025.10.21 |
|---|---|
| [Linux/Unix][4-1] UNIX ํ์ผ ์ข ๋ฅ (0) | 2025.10.21 |
| [Linux/Unix][3-4] bash ํ์ผ / ๋งค๊ฐ๋ณ์ ํ์ฅ ๋ฌธ๋ฒ / ${:-}, ${:=}, ${:+}, ${:?} (0) | 2025.10.21 |
| [Linux/Unix][3-3] ๐งฉ ํ์ผ ๊ถํ ๋ฐ ์์ ๊ถ ๊ด๋ฆฌ ๋ช ๋ น์ด: umask, chown, chgrp (0) | 2025.10.21 |
| [Linux/Unix][3-2] chmod ๋ช ๋ น (0) | 2025.10.21 |