1 篇文章带有标签 “function”

Linux Shell 实践

  • 执行时的所有信息都输出到文件(&>)
echo hello &> log.info
$ cat log.info 
hello
  • 创建一个文件并写入内容(> filename <<EOF)
cat > hello.sh << EOF
#!/bin/bash
echo hello
EOF
  • 整数四则运算(let)
let n=10-3+4/2
echo $n
9
  • 四种执行模式运行
$ vim test.sh
#!/bin/bash
echo $str
$ chmod u+x test.sh
$ bash test.sh 
$ ./test.sh 
$ source test.sh 
hello world
$ . test.sh 
hello world
  • 导出变量(export),父进程定义的变量子进程可见。
$ export str
$ bash test.sh 
hello world
$ source test.sh 
hello world
  • 移除变量(unset)
$ unset str
$ echo $str
  • $$ 当前进程ID
$ echo $$
26102
  • $0 当前进程名
$ echo $0
-bash
  • 显示所有数组元素
$ echo ${ips[@]}
10.0.0.1 10.0.0.2 10.0.0.3