shebang的使用

shebang的使用

定义

##shebang是首行以#!开头的语句,默认使用什么解释器解释以下语句

#!/usr/bin/bash

#!/usr/bin/python



[root@hecs-98663 myshell]# which python
/usr/bin/python
[root@hecs-98663 myshell]# which bash
/usr/bin/bash

使用

## 如果不加shebang,解释器默认使用/bin/bash执行
##以下两种写法是相同的
[root@hecs-98663 myshell]# echo $SHELL
/bin/bash

[root@hecs-98663 myshell]# cat a.sh 
echo "hello world"
[root@hecs-98663 myshell]# cat aa.sh 
#!/bin/bash
echo "hello world"

[root@hecs-98663 myshell]# chmod +x a.sh 
[root@hecs-98663 myshell]# chmod +x aa.sh 
[root@hecs-98663 myshell]# ./a.sh 
hello world
[root@hecs-98663 myshell]# ./aa.sh 
hello world

#可以使用自己的解释器来执行
[root@hecs-98663 myshell]# vim b.py
[root@hecs-98663 myshell]# cat b.py 
#!/usr/bin/python
print("hello world")
[root@hecs-98663 myshell]# python b.py 
hello world
[root@hecs-98663 myshell]# chmod +x b.py 
[root@hecs-98663 myshell]# ./b.py 
hello world


##如果shebang写错,则无法自动使用,需要自己添加
[root@hecs-98663 myshell]# vim b.py 
[root@hecs-98663 myshell]# cat b.py 
#!/usr/bin/python111
print("hello world")
[root@hecs-98663 myshell]# ./b.py 
-bash: ./b.py: /usr/bin/python111: 坏的解释器: 没有那个文件或目录
[root@hecs-98663 myshell]# python b.py 
hello world

posted @ 2023-11-23 09:42  Bre-eZe  阅读(29)  评论(0)    收藏  举报