系列目录与参考文献传送门: shell入门学习笔记-序章
命令详解
admindeMacBook-Pro:myshell admin$ type cd
cd is a shell builtin
admindeMacBook-Pro:myshell admin$ type ifconfig
ifconfig is /sbin/ifconfig
admindeMacBook-Pro:myshell admin$ type ls
ls is hashed (/bin/ls)
admindeMacBook-Pro:myshell admin$ echo $PATH
/Users/admin/local/apache-maven-3.5.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
admindeMacBook-Pro:myshell admin$ cd /usr/bin
- 通过
type
来查看命令来源/类型。 - 内建命令执行速度相对快,因为执行内建命令相当于调用shell进程的一个函数。
- 外部命令执行速度相对慢,因为执行外部命令需要出发磁盘I/O,需要fork出单独的进程,然后退出进程。
命令 | 命令 | 命令 | 命令 | 命令 | 命令 |
---|---|---|---|---|---|
bash | : | . | [ | alias | bg |
break | builtin | cd | command | compgen | complete |
declare | dirs | disown | echo | enable | eval |
exit | export | fc | fg | getopts | hash |
history | jobs | kill | let | local | logout |
printf | pushd | pwd | read | readonly | return |
shift | shopt | source | suspend | test | times |
type | typeset | ulimit | umask | unalias | unset |
alias命令别名
- 在当前shell会话创建的命令别名是全局变量,而非环境变量,关闭session会失效。
- 命令别名生效的方式是写入至配置中,一般情况下是文件
.bashrc
文件,mac上是.bash_profile
。 - 造成这些区别的原因是 Mac 的 bash 是一个 login bash,login bash会加载的是
.bash_profile
不会加载.bashrc
文件。
admindeMacBook-Pro:~ admin$ ls ~/.bash_profile
/Users/admin/.bash_profile
admindeMacBook-Pro:~ admin$ cat ~/.bash_profile
全局命令别名
admindeMacBook-Pro:~ admin$ alias showFileList="ls -l"
admindeMacBook-Pro:~ admin$ showFileList
total 1917544
drwx------@ 3 admin staff 96 6 19 2018 Applications
drwx------+ 31 admin staff 992 1 4 15:36 Desktop
drwx------+ 14 admin staff 448 8 28 20:58 Documents
环境变量别名
admindeMacBook-Pro:~ admin$ vi ~/.bash_profile
admindeMacBook-Pro:~ admin$ cat ~/.bash_profile
alias now="echo $(date)"
admindeMacBook-Pro:~ admin$ now
-bash: now: command not found
admindeMacBook-Pro:~ admin$ source ~/.bash_profile # 通过source使其生效
admindeMacBook-Pro:~ admin$ now
2019年 1月 7日 星期一 17时25分34秒 CST
admindeMacBook-Pro:~ admin$ sleep 1
admindeMacBook-Pro:~ admin$ now
2019年 1月 7日 星期一 17时25分34秒 CST
admindeMacBook-Pro:~ admin$ source ~/.bash_profile
admindeMacBook-Pro:~ admin$ now
2019年 1月 7日 星期一 17时29分14秒 CST
- 记得通过
source
使其在本次shell会话中生效。
移除命令别名
admindeMacBook-Pro:~ admin$ alias
alias now='echo 2019年 1月 7日 星期一 17时29分14秒 CST'
alias showDir='pwd'
alias showFileList='ls -l'
# 移除指定命令别名
admindeMacBook-Pro:~ admin$ unalias showDir
admindeMacBook-Pro:~ admin$ alias
alias now='echo 2019年 1月 7日 星期一 17时29分14秒 CST'
alias showFileList='ls -l'
# 移除当前shell会话中的所有命令别名(包含.bash_profile定义的)
admindeMacBook-Pro:~ admin$ unalias -a
admindeMacBook-Pro:~ admin$ alias
admindeMacBook-Pro:~ admin$
admindeMacBook-Pro:~ admin$ source ~/.bash_profile
admindeMacBook-Pro:~ admin$ alias
alias now='echo 2019年 1月 7日 星期一 17时33分03秒 CST'
# 新的shell会话
admindeMacBook-Pro:~ admin$ alias
alias now='echo 2019年 1月 7日 星期一 17时30分20秒 CST'
admindeMacBook-Pro:~ admin$
- 通过
unalias myName
移除指定的命令别名。 - 通过
unalias -a
移除当前shell会话中的所有命令别名(包含.bash_profile
定义的)。 unalias -a
不会修改.bash_profile
文件内容,通过source或者新开shell会话,会再次生效。
exit退出
- 退出shell会话
- 退出脚本
admindeMacBook-Pro:myshell admin$ cat a.sh
#!/bin/bash
echo "before exit"
exit 8
echo "after exit"
admindeMacBook-Pro:myshell admin$ ./a.sh
before exit
admindeMacBook-Pro:myshell admin$ echo $?
8
- 如果不设置exit之后的状态值,则默认返回0。
- 可以通过
$?
获取脚本退出的返回值。 - 此种情况下,正确执行脚本的方式只能是将其当成可执行文件:
./a.sh
或者/bin/bash a.sh
。 - 如果执行
. a.sh
或者`source a.sh·,则会退出当前shell会话。