1.1 脚本认识
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
- [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
来源: https://www.cnblogs.com/jsjobs/p/9471837.html