bash脚本
Bash scripting cheatsheet (devhints.io)
又是一个好网站

bash脚本始终以
#!/bin/bash开头

变量创建

name="123"
不能有空格,使用的时候要加上$

echo $name

调试

set -x开头,set +x结束,中间插入shell脚本
如果出现错误,该行会输出一个-

$0代表执行的文件
$#代表传入的参数个数

bash和zsh下的元素索引不一样

bash默认0开始,zsh默认1开始

cars=('honda' 'audi' 'bmw' 'tesla')
echo "${cars[0]}"
unset cars[0] //honda => ''
Operator 操作员 Description 描述
-eq Checks if the value of two operands are equal or not; if yes, then the condition becomes true.
检查两个操作数的值是否相等;如果是,则条件成立。
-ne Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.
检查两个操作数的值是否相等;如果值不相等,则条件成立。
-gt Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true.
检查左操作数的值是否大于右操作数的值;如果是,则条件成立。
-lt Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.
检查左操作数的值是否小于右操作数的值;如果是,则条件成立。
-ge Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true.
检查左操作数的值是否大于或等于右操作数的值;如果是,则条件成立。
The -f checked if the file existed.
-f 检查文件是否存在。

The -w checked if the file was writable, without write permissions we wouldn’t be able to output our text into the file.
-w 检查文件是否可写,如果没有写权限,我们将无法将文本输出到文件中。