shell入门学习笔记-11-命令详解: alias、exit

系列目录与参考文献传送门: 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:.[aliasbg
breakbuiltincdcommandcompgencomplete
declaredirsdisownechoenableeval
exitexportfcfggetoptshash
historyjobskillletlocallogout
printfpushdpwdreadreadonlyreturn
shiftshoptsourcesuspendtesttimes
typetypesetulimitumaskunaliasunset

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会话。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值