本系列文章主要是指在大多Linux发型版下默认Bash Shell。Linux习题是RedHat下的CentOS操作系统,完全免费。与其商业版RHEL(Red Hat Enterprise Linux)出自同样的源代码,不同的是CentOS并不包含封闭源代码软件和售后支持。
用vi打开test.sh编写
[root@lu ~]# vi test.sh
#!/bin/bash
echo "Hello World!"
- 第1行指定解释器
- 第2行打印Hello World!
写好脚本后,开始执行,执行Shell脚本有3种方法:
方法1:直接用bash解释器执行
[root@lu ~]# bash test.sh
Hello World!
当前终端会新生成一个子bash去执行脚本。
方法2:添加可执行权限
[root@lu ~]# ll test.sh
-rw-r--r-- 1 root root 32 Dec 19 22:16 test.sh
[root@lu ~]# chmod +x test.sh
[root@lu ~]# ./test.sh
Hello World!
这种方法默认根据脚本第一行指定的解释器处理,如果没写以当前默认Shell解释器执行。
方法3:source命令执行,以当前默认Shell解释器执行
[root@lu ~]# source test.sh
Hello World!
下一篇:3.Shell基础知识-Shell变量-1