1 #!/bin/bash
第一行, 通常用来指定执行脚本的 shell ,/bin/bash 是 CentOS 默认的 shell 如果写到第二行, 就是注释了
1 #!/bin/sh
这样写也是可以的, sh 是 bash 的软链接, 和 #!/bin/bash 是没有区别的, 理规范的开头使用 #!/bin/bash
1.2 查看 bash 版本
- [root@oldboy-lesson scripts]# bash --version
- GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
- Copyright (C) 2009 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
- This is free software; you are free to change and redistribute it.
- There is NO WARRANTY, to the extent permitted by law.
- [root@oldboy-lesson scripts]# cat test_1.sh
- #!/bin/bash
- user=`whoami`
- echo $user
- [root@oldboy-lesson scripts]# sh test_1.sh
- root
- [root@oldboy-lesson scripts]# ./test_1.sh
- root
- [root@oldboy-lesson scripts]# . test_1.sh
- root
- [root@oldboy-lesson scripts]# source test_1.sh
- root
- [root@oldboy-lesson scripts]# /bin/bash test_1.sh
- root
- [root@oldboy-lesson scripts]# /bin/sh test_1.sh
- root
- [root@oldboy-lesson scripts]# /server/scripts/test_1.sh
- root
1.3 书写脚本
1.3.1 执行脚本
1.3.1.1 小结:
执行脚本的方法有: sh ./ . source /bin/bash /bin/sh pwd/
1.4 继承脚本变量
- [root@oldboy-lesson scripts]# sh test_1.sh
- root
- [root@oldboy-lesson scripts]# echo $user
- [root@oldboy-lesson scripts]# ./test_1.sh
- root
- [root@oldboy-lesson scripts]# echo $user
- [root@oldboy-lesson scripts]# bash test_1.sh
- root
- [root@oldboy-lesson scripts]# echo $user
- [root@oldboy-lesson scripts]# . test_1.sh
- root
- [root@oldboy-lesson scripts]# echo $user
- root
- [root@oldboy-lesson scripts]# source test_1.sh
- root
- [root@oldboy-lesson scripts]# echo $user
- root
1.4.1.1 小结
继承: source 和 . 能继承脚本的变量, 其它命令皆不能继承
初识脚本
来源: http://www.bubuko.com/infodetail-2726912.html