必须在脚本文件开头写下:
- #!/bin/bash
调用一个基本的Linux命令,
- echo "hello,world"
保存为test.sh文件,打开执行权限:
- sudo chmod +x ./test.sh
./test.sh
看到了hello,world.
添加一个注释吧,用#开头(pound sign)的所有字符都是注释,除了第一行以外
- #!/bin/bash
- echo "hello,world" #this is a test
bash提供了一些预定义的变量供编程使用,具体可参考文档:
http://www.gnu.org/software/bash/manual/bash.html#Bash-Variables
这里使用几个:
- #!/bin/bash
- echo "hello,world" #this is a test
- echo $BASH
- echo $LANG
- echo $USER
- echo $PWD
- chenshu@sloop2:~/Ubuntu One$ ./test.sh
- hello,world
- /bin/bash
- en_US.UTF-8
- chenshu
- /home/chenshu/Ubuntu One
现在自己定义个变量,然后用一下试试看。
- me="ok"
- echo $me
$符是取变量的值的运算符。
如果字符串中有空格,才必须用" "括起来,否则可以不用。
因为bash会把空格视作两个命令之间的分隔符。
