Shell 是一个用 C 语言编写的程序,通过 Shell 用户可以访问操作系统内核服务。
Shell 既是一种命令语言,又是一种程序设计语言。
Shell script 是一种为 shell 编写的脚本程序。Shell 编程一般指 shell脚本编程,不是指开发 shell 自身。
Shell 编程跟 java、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。
Linux 的 Shell 解释器 种类众多,一个系统可以存在多个 shell解释器,可以通过 cat /etc/shells 命令查看系统中安装的 shell解释器。
Bash 由于易用和免费,在日常工作中被广泛使用。同时,Bash 也是大多数Linux 系统默认的 Shell。
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
bash -version
$ bash -version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
#!/usr/bin/env bash
让程序通过$PATH(路径环境变量)去查找第一个遇到的shell解释器,此做法避免各系统间系统shell解释器存放于不同的目录。而不是写死相应目录,例如:#! /bin/bash
就是假设系统默认的bash解释器在/bin/中。
${}指明变量边界.
比如:需要在生日时间后面拼接一个Day字符串
birth='99-10-10'
echo $birthDay
会输出不来结果,因为解释器去查找birthDay这个变量去了.
正确做法应该是 echo ${birth}Day
两者都是命令替换,$()只是``的一种替代方案
比如:
uver=$(uname -r)
uver=`uname -r`
三者都是test命令的表现形式 test num -ge 100
,也就是三者本质都是test命令
[] 是 POSIX 标准的测试命令, 支持基本的文件和字符串测试操作符,但不支持复杂的逻辑操作。
- []需要括号的两个内侧都有空格
- []比较类型必须是数字类型
- 运算符需要转义
- 必须使用$变量符
num=10
if [ $num -ge 100 ];then
echo "big than special number"
fi
# 导出一个环境变量NVM_DIR,它代表了用户根目录下的.nvm目录
8000
span>
export NVM_DIR="$HOME/.nvm"
# -s 文件是否存在且内容不为空
# [] 是test命令
# . 是source命令
# 所以整行命令的意思是:如果$NVM_DIR/nvm.sh脚本存在且内容不为空,就source "$NVM_DIR/nvm.sh"执行这个脚本
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
- 两侧对空格不再有要求
- 比较类型必须是数字
- 不支持转义字符
- 变量可以不加$符号
- 相对[]拓展了以下运算符的支持
运算符 | 含义 |
---|---|
id++ id-- | variable post-increment and post-decrement |
++id --id | variable pre-increment and pre-decrement |
- + | unary minus and plus |
! ~ | logical and bitwise negation |
** | exponentiation |
* / % | multiplication, division, remainder |
+ - | addition, subtraction |
<< >> | left and right bitwise shifts |
<= >= < > | comparison |
== != | equality and inequality |
& | bitwise AND |
^ | bitwise exclusive OR |
| | bitwise OR |
&& | logical AND |
|| | logical OR |
expr ? expr : expr | conditional operator |
= *= /= %= += -= <<= >>= &= ^= |= | assignment |
expr1 , expr2 | comma |
参考文档: https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html
num=10
if ((++num == 10)); then
echo "数字相等"
else
echo "数字不相等"
fi
num=10
if (($((++num)) == 10)); then
echo "数字相等"
else
echo "数字不相等"
fi
[[ ]] 是 Bash 和其他一些现代 Shell(如 Zsh)提供的扩展测试命令。 支持更多的操作符和复杂的逻辑操作,如正则表达式匹配和逻辑组合。
- 需要括号的两个内侧都有空格
- 比较类型支持数字和字符串
- 运算符转义和不转义都可正常使用
- 必须使用$变量符
- 支持模式匹配,如下
运算符 | 描述 |
---|---|
-a file | True if file exists. |
-b file | True if file exists and is a block special file. |
-c file | True if file exists and is a character special file. |
-d file | True if file exists and is a directory. |
-e file | True if file exists. |
-f file | True if file exists and is a regular file. |
-g file | True if file exists and its set-group-id bit is set. |
-h file | True if file exists and is a symbolic link. |
-k file | True if file exists and its "sticky" bit is set. |
-p file | True if file exists and is a named pipe (FIFO). |
-r file | True if file exists and is readable. |
-s file | True if file exists and has a size greater than zero. |
-t fd | True if file descriptor fd is open and refers to a terminal. |
-u file | True if file exists and its set-user-id bit is set. |
-w file | True if file exists and is writable. |
-x file | True if file exists and is executable. |
-G file | True if file exists and is owned by the effective group id. |
-L file | True if file exists and is a symbolic link. |
-N file | True if file exists and has been modified since it was last read. |
-O file | True if file exists and is owned by the effective user id. |
-S file | True if file exists and is a socket. |
file1 -ef file2 | True if file1 and file2 refer to the same device and inode numbers. |
file1 -nt file2 | True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not. |
file1 -ot file2 | True if file1 is older than file2, or if file2 exists and file1 does not. |
-o optname | True if the shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin (see The Set Builtin). |
-v varname | True if the shell variable varname is set (has been assigned a value). |
-R varname | True if the shell variable varname is set and is a name reference. |
-z string | True if the length of string is zero. |
-n string | |
string | True if the length of string is non-zero. |
string1 == string2 | |
string1 = string2 | True if the strings are equal. When used with the [[ command, this performs pattern matching as described above (see Conditional Constructs).‘=’ should be used with the test command for POSIX conformance. |
string1 != string2 | True if the strings are not equal. |
string1 < string2 | True if string1 sorts before string2 lexicographically. |
string1 > string2 | True if string1 sorts after string2 lexicographically. |
arg1 OP arg2 | OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers. When used with the [[ command, Arg1 and Arg2 are evaluated as arithmetic expressions (see Shell Arithmetic). |
参考文档: https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
num=101
if [[ $num > 100 ]]; then
echo "big than special number"
fi
if [[ -a "/Users/tobbyquinn/github/ShellStudy/Examples1.sh" ]];then
echo "文件存在"
else
echo "文件不存在"
fi
[[ -f "$HOME/.fig/shell/zshrc.post.zsh" ]] && builtin source "$HOME/.fig/shell/zshrc.post.zsh"
- 都是进行算术运算的
- 两者都支持变量不加$
注意:优先使用$(()),因为$[]并没有在bash手册中提及
a1=10
a2='20'
echo $[$a1+$a2] #支持
echo $[a1+a2] #支持
echo $((a1+a2)) #优先使用,支持
echo $(($a1+$a2)) #优先使用,支持