본문 바로가기

TIL/개인공부

[bash] shebang이란?

반응형

shebang이란?

sharp(#) + bang (!) 합성어이며 Unix 계열 OS에서 스크립트 코드 최상단에서 해당 파일을 해석해줄 인터프리터의 절대경로를 지정해주는 기능을 갖는다.

* 근데 왜 'sha'rp 인데 'she'bang으로 된거지...

 

일반적으로 사용하는 shebang

#!/usr/bin/env bash
#!/usr/bin/env python
#!/bin/bash
...

사용방법

bash같은경우에는 shebang없이 bash script를 작성하고 실행하기 위해서는 다음과같은 명령어로 진행된다.

bash test.sh

하지만 test.sh에 shebang을 추가하고 실행권한을 부여한다면 아래와같이 실행을 할수있다.

# test.sh에는 "#!/usr/bin/env bash" 라는 shebang이 추가됨
chmod +x test.sh
./test.sh

추천하는 shebang

사실 이글을 작성하기 전까지는 #!/bin/bash라는 shebang을 사용했는데, 아래 글들을 읽어보니 #!/usr/bin/env bash를 추천하는것같다.

이유는 일단 제일 범용적으로 사용할수있기때문이라고 한다. 예를들어서 특정 OS에는 bash라는 실행파일이 /bin폴더 아래에 없다던지...등등사유가 있다.

 

https://en.wikipedia.org/w/index.php?title=Shebang_(Unix)&oldid=878552871#Portability 

 

Shebang (Unix) - Wikipedia

From Wikipedia, the free encyclopedia In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. It is also called sha-bang,[1][2] hashbang,[3][4] pound-bang,[5][6] or

en.wikipedia.org

https://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang

 

What is the preferred Bash shebang ("#!")?

Is there any Bash shebang objectively better than the others for most uses? #!/usr/bin/env bash #!/bin/bash #!/bin/sh #!/bin/sh - etc I vaguely recall a long time ago hearing that adding a dash t...

stackoverflow.com

 

반응형