shell脚本5种运行方式
案例:print.sh
[root@ping ~]# cat print.sh
#!/bin/sh
echo "Hello World!"
1.指定shell运行
[root@ping ~]# sh print.sh
Hello World!
注释:
- 根据定义的shell环境,直接运行脚本。
- shell有
bash,sh,ash,bsh,csh,ksh,tcsh
等。 - 格式:
# [bash] [filename.sh]
- 需要开启一个shell子进程
- 环境声明可省略,执行权限可省略
2.以“ ./ ”来运行
[root@ping ~]# chmod +x print.sh
[root@ping ~]# ./print.sh
Hello World!
注释:
- 开启shell子进程执行
3.以绝对路径 运行
[root@ping ~]# chmod +x print.sh
[root@ping ~]# /root/print.sh
Hello World!
注释:
- 开启shell子进程执行
4.以“ . ”来运行
[root@ping ~]# . print.sh
Hello World!
注释:
- 当前shell执行
5.以“source”来运行
[root@ping ~]# source print.sh
Hello World!
注释:
- 当前shell执行