리눅스에서 쉘스크립트를 확인하다 보면 볼 수 있는 인자값 $*과 $@이 있다.

검색해보면 둘 다 인자값을 넘겨주는 변수인데 어떤 차이가 있는지 알아보았다.

 

$* expands to all parameters that were passed to that shell script.
$0 = shell script's name
$1 = first argument
$2 = second argument ...etc
$# = number of arguments passed to shellscript

 

It means all the arguments passed to the script or function, split by word.
It is usually wrong and should be replaced by "$@", which separates the arguments properly

 

리눅스에서 사용되는 명령어들은 인자값을 받을 수 있다.

예를들면 도커 이미지를 실행하는 명령어는 다음과 같다.

docker start [images]

위의 명령어를 보면 docker 명령어에, start와 images 인자를 함께 전달하여 명령어를 실행한 것인데

 

$* 같은 경우는 넘어온 모든 인자들을 하나의 문자열로 보기 때문에

[start images]라고 인식을 하며

 

$@ 같은 경우는 넘오온 인자들을 각각의 인자로 인식하기 때문에

start / images 로 인식하게 된다.

 

위에 언급한 인용문처럼 일반적으로 $*는 잘못된 사용법이며, $@로 바꿔야 한다고 이야기하고 있다.

 

참고

 

https://stackoverflow.com/questions/12413848/what-does-mean-in-a-shell-script

 

what does $* mean in a shell script

What does $* exactly mean in a shell script? For example consider the following code snippet $JAVA_HOME/bin/java/com/test/Testclass $*

stackoverflow.com

 

 

'IT > VS' 카테고리의 다른 글

[JavaScript] NPM vs Yarn  (0) 2023.07.14
[Java] StringBuilder VS BufferedWriter  (0) 2023.04.02
[ VS]arguments VS parameter  (0) 2021.09.23

+ Recent posts