1. 注释
Example:# This is a comment
2. 转义字符
Example:
rm *
ls ??
cat file[1-3]
echo "How are you?"
echo "Hello world"
variable_name=value
declare variable_name=value
name="John Doe"
x=5
注:等号两边不能有空格存在
export VARIABLE_NAME=value
declare -x VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.
echo $variable_name
echo $name
echo $PATH
read variable_name
read name1 name2 ...
$scriptename arg1 arg2 arg3
在脚本内可以使用如下方式访问参数
echo $1 $2 $3 输出参数arg1, arg2, arg3
echo $* 输出所有参数
echo $# 输出参数个数
declare -a array_name=(world1 world2 world3 ...)
declare -a fruit=(apples pears plums)
echo ${fruit[0]}
variable_name=`command`
variable_name=$(command)
declare -i variable_name
typeset -i variable_name
((n=5+5))
echo $n
!=
>
>=
<
<=
then
block of statements
fi
if command
then
block of statements
elif command
then
block of statements
fi
if [[ expression ]]
then
block of statements
fi
if (( numeric expression ))
then
block of statements
else
block of statements
fi
case variable_name in
pattern1)
statements
;;
pattern2)
statements
;;
pattern3)
statements
;;
esac
while command
do
block of statements
done
until command
do
block of statements
done
while [[ string expression ]]
do
block of statements
done
until [[ string expression ]]
do
block of statements
done
for variable in word_list
do
block of statements
done
select variable in word_list
do
block of statement
done
function_name() {
block of code
}
function function_name {
block of code
}
=== end ===
ls ??
cat file[1-3]
echo "How are you?"
3. 输出
Example:echo "Hello world"
3. 局部变量
Example:
variable_name=value
declare variable_name=value
name="John Doe"
x=5
注:等号两边不能有空格存在
4. 全局变量
Example:
export VARIABLE_NAME=value
declare -x VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.
5. 变量输出
Example:
echo $variable_name
echo $name
echo $PATH
6. 输入
Example:
read variable_name
read name1 name2 ...
7. 参数
Example:
$scriptename arg1 arg2 arg3
在脚本内可以使用如下方式访问参数
echo $1 $2 $3 输出参数arg1, arg2, arg3
echo $* 输出所有参数
echo $# 输出参数个数
8. 数组
Example:
declare -a array_name=(world1 world2 world3 ...)
declare -a fruit=(apples pears plums)
echo ${fruit[0]}
9. 命令
Example:
variable_name=`command`
variable_name=$(command)
10. 算术操作
Example:
declare -i variable_name
typeset -i variable_name
((n=5+5))
echo $n
11. test命令操作符
==
!=
>
>=
<
<=
12. if条件语句
Example:if command
then
block of statements
fi
if command
then
block of statements
elif command
then
block of statements
fi
if [[ expression ]]
then
block of statements
fi
if (( numeric expression ))
then
block of statements
else
block of statements
fi
13. case语句
Example:
case variable_name in
pattern1)
statements
;;
pattern2)
statements
;;
pattern3)
statements
;;
esac
14. 循环
Example:
while command
do
block of statements
done
until command
do
block of statements
done
while [[ string expression ]]
do
block of statements
done
until [[ string expression ]]
do
block of statements
done
for variable in word_list
do
block of statements
done
select variable in word_list
do
block of statement
done
15. 函数
Example:
function_name() {
block of code
}
function function_name {
block of code
}
=== end ===