"쉘 프로그래밍 변수"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 7개는 보이지 않습니다)
10번째 줄: 10번째 줄:
* 변수 할당 시점과 달리 사용시에는 $를 붙여 주어야함
* 변수 할당 시점과 달리 사용시에는 $를 붙여 주어야함
* 변수 할당 시 공백은 없이 작성해야 함
* 변수 할당 시 공백은 없이 작성해야 함
<source lang="bash">
* 변수를 ""로 묶어 줄 수 있음
* $ 문자의 출력을 위해서는 ''로 묶거나 앞에 \를 붙여 이스케이프 해줘야함
<syntaxhighlight lang='bash'>
변수명=value # (O)
변수명 =value # (X)
변수명= value # (X)
변수명 = value # (X)
</syntaxhighlight>
 
==예시==
;현재 디렉토리의 값을 변수에 담아 출력
<syntaxhighlight lang="bash">
#!/bin/sh
#!/bin/sh
curDir=$PWD
curDir=$PWD
echo $curDir
echo $curDir
</source>
</syntaxhighlight>
:1) 현재 디렉토리 값을 변수에 할당한 후 그 변수 값을 출력
:1) 현재 디렉토리 값을 변수에 할당한 후 그 변수 값을 출력
:2) 출력시에는 $를 붙여줌
:2) 출력시에는 $를 붙여줌
22번째 줄: 33번째 줄:
! 문자 !! 설명
! 문자 !! 설명
|-
|-
| $1 ~ $n || n에 기입된 숫자의 위치 매개변수 참조
| $1 ~ $n || n에 기입된 숫자의 위치 인자 참조
|-
|-
| $# || 스크립트에 넘겨진 인자의 개수
| $# || 스크립트에 넘겨진 인자의 개수
29번째 줄: 40번째 줄:
|}
|}


<source lang="bash">
<syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash
echo 1st argument: $1
echo 1st argument: $1
35번째 줄: 46번째 줄:
echo 3rd argument: $3
echo 3rd argument: $3
echo number of arguments: $#
echo number of arguments: $#
</source>
</syntaxhighlight>
<source lang="console">
<syntaxhighlight lang="console">
testuser@zetawiki:~$ sh test.sh a b c
testuser@zetawiki:~$ sh test.sh a b c
1st argument: a
1st argument: a
42번째 줄: 53번째 줄:
3rd argument: c
3rd argument: c
number of arguments: 3
number of arguments: 3
</source>
</syntaxhighlight>
<source lang="console">
<syntaxhighlight lang="console">
testuser@zetawiki:~$ sh test.sh "a b c" 'd e' f g
testuser@zetawiki:~$ sh test.sh "a b c" 'd e' f g
1st argument: a b c
1st argument: a b c
49번째 줄: 60번째 줄:
3rd argument: f
3rd argument: f
number of arguments: 4
number of arguments: 4
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[리눅스 $0]]
* [[리눅스 $0]]
* [[리눅스 현재 쉘 확인]]
* [[리눅스 현재 쉘 확인]]
* [[리눅스 pwd]]


==참고 자료==
==참고==
* https://linux.die.net/Bash-Beginners-Guide/sect_10_01.html
* https://linux.die.net/Bash-Beginners-Guide/sect_10_01.html
* https://wiki.kldp.org/wiki.php/ShellProgrammingTutorial
* https://wiki.kldp.org/wiki.php/ShellProgrammingTutorial


[[분류: Bash]]
[[분류: Bash]]

2020년 11월 2일 (월) 02:40 기준 최신판

1 개념[ | ]

Shell Programming Variable
쉘 프로그래밍 변수

2 변수 만들기[ | ]

  • "="로 값 할당
  • 자료형은 주로 문자열
bash 버전에 따라 상수, 배열 등도 사용가능
  • 변수 선언을 필요로 하지는 않음
  • 변수 할당 시점과 달리 사용시에는 $를 붙여 주어야함
  • 변수 할당 시 공백은 없이 작성해야 함
  • 변수를 ""로 묶어 줄 수 있음
  • $ 문자의 출력을 위해서는 로 묶거나 앞에 \를 붙여 이스케이프 해줘야함
변수명=value # (O)
변수명 =value # (X)
변수명= value # (X)
변수명 = value # (X)

3 예시[ | ]

현재 디렉토리의 값을 변수에 담아 출력
#!/bin/sh
curDir=$PWD
echo $curDir
1) 현재 디렉토리 값을 변수에 할당한 후 그 변수 값을 출력
2) 출력시에는 $를 붙여줌

4 특수 변수[ | ]

문자 설명
$1 ~ $n n에 기입된 숫자의 위치 인자 참조
$# 스크립트에 넘겨진 인자의 개수
$0 명령 라인에서는 쉘명, 스크립트에서는 파일 경로를 포함한 파일명
#!/bin/bash
echo 1st argument: $1
echo 2nd argument: $2
echo 3rd argument: $3
echo number of arguments: $#
testuser@zetawiki:~$ sh test.sh a b c
1st argument: a
2nd argument: b
3rd argument: c
number of arguments: 3
testuser@zetawiki:~$ sh test.sh "a b c" 'd e' f g
1st argument: a b c
2nd argument: d e
3rd argument: f
number of arguments: 4

5 같이 보기[ | ]

6 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}