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

版权声明:本文为博主hanchao5272原创文章,转载请注明来源,并留下原文链接地址,谢谢! https://blog.csdn.net/hanchao5272/article/details/86562038

系列目录与参考文献传送门: 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会话。

猜你喜欢

转载自blog.csdn.net/hanchao5272/article/details/86562038