variable
That statement by
a=2
b="123"
Call
$ {varName} or $ varName
echo $b
echo ${a}
Common variable
- The $ ?: determine whether a statement success
- $ 0: execute the script file name
- $ 1: The first argument
- $ N: n th argument
- $ *: What are the parameters
- $ #: The number of parameters
Examples
demo1.sh
echo "脚本文件名称:$0"
echo "第1个参数:$1"
echo "所有的参数:$*"
echo "参数个数:$#"
Given execute permissions
chmod u+x demo1.sh
carried out
./demo1.sh param2 1 2 3
Output
脚本文件名称:./demo1.sh
第1个参数:param2
所有的参数:param2 1 2 3
参数个数:4
symbol
Common Symbols
>
: Write to target file
- It will overwrite the original content
Examples: hello world string write to the file 1.txt
# 直接在bash中可运行 echo hello world > 1.txt
>>
: Insert the end of the file content to the target
- In the end of the file additional content source
Example: add a line to the file 1.txt string hello world
# 直接在bash中可运行 echo hello world >> 1.txt
;
: Execute multiple instructions
Example: an additional line of text and then displays the current contents of the file
# 直接在bash中可运行 echo hello world >> 1.txt ; cat 1.txt
|
: Pipe symbol
- Result of the processing on a command passed to the next command processing
Example: cat results to statistics wc command line
# 直接在bash中可运行 # 最后一个字母为小写的L cat 1.txt | wc -l
&&
: Execute multiple commands
- Before a command error, block the subsequent command
Example: Append a single line to view the number of rows and then 1.txt
# 直接在bash中可运行 echo str >> 1.txt && wc -l 1.txt
||
: Execute multiple commands
- The previous command will execute the subsequent instruction error
Example: Append content to 1.txt line error, and then view its contents
# 直接在bash中可运行 echo3 str >> 1.txt || cat 1.txt
""
: Output variable values can be
- Able to parse the string variable $ varName
Example: Output hello
# 编写demo3.sh a="hello" echo "$a"
''
: Output variable values can not be
- Not enough to resolve $ varName variable string
Examples: can not output hello
# 编写demo3.sh a="hello" echo '$a'
The output is: $ a
``
: Output command result
- Example: output current date
date command to get the current date
echo `date`
2>/dev/null
: Error output to the bottomless pit
- Command execution error does not occur error
- The implementation will have correct output
example
# 执行 echo1 123 # 正常错误输出 bash: echo1: 未找到命令 # 使用无底洞 echo1 123 2>/dev/null # 没有任何内容输出
1>/dev/null
: Correct output to the bottomless pit
- Command execution results are not displayed correctly
- Execution errors will have an error message content
example
# 执行 echo 123 # 正常错误输出 123 # 使用无底洞 echo 123 1>/dev/null # 没有任何内容输出