Shell脚本执行的四种方法

(1).bash(或sh) [脚本的相对路径或绝对路径]

[xf@xuexi ~]$ cat a.sh
#!/bin/bash
echo "hello world!"
[xf@xuexi ~]$ bash a.sh
hello world!
[xf@xuexi ~]$ which bash
/usr/bin/bash
[xf@xuexi ~]$ sh a.sh
hello world!
[xf@xuexi ~]$ which sh
/usr/bin/sh
[xf@xuexi ~]$ ll /usr/bin/sh
lrwxrwxrwx. 1 root root 4 4月   5 22:07 /usr/bin/sh -> bash

  可以看到sh命令实际指向的是bash命令,sh只是bash的一个软链接

(2).source(或.) [脚本的相对路径或绝对路径]

  .(点)是等同于source的,最常见的使用是在重新加载环境变量时。

[xf@xuexi ~]# source a.sh
hello world!
[xf@xuexi ~]# . a.sh
hello world!

 

(3).sh < [脚本名称]或者cat [脚本名称] | sh(或bash)

[xf@xuexi ~]# sh < a.sh
hello world!
[xf@xuexi ~]# cat a.sh | sh
hello world!

(4).脚本的相对路径或绝对路径,使用该方法,脚本必须拥有执行权限

[xf@xuexi ~]$ chmod +x a.sh
[xf@xuexi ~]$ ./a.sh 
hello world!
[xf@xuexi ~]$ /home/xf/a.sh 
hello world!

  

posted @ 2019-05-03 16:11  苦逼运维  阅读(3582)  评论(0编辑  收藏  举报