「Bash」- 杂记

  CREATED BY JENKINSBOT

Bash 是一个 sh 兼容的命令语言解释器,执行标准输入或文件中的命令。Bash 还包含了 ksh 和 csh 的有用功能。Bash 最终旨在成为 IEEE Standard 1003.2 的一致性实现;

终端下的提示功能在 Bash Completion 软件包中;

输入特殊字符

比如输入一个 TAB 键,但是 Bash 中 TAB 用于补全提示。。;

先按下 Ctrl+V,然后在按下 TAB 键;

当 BASH 无响应时

使用 Ctrl+J 组合键;或者输入 reset 内建命令;

在命令提示符号中显示当前 GIT 分支

Display git branch in bash prompt

# 方法一:
export PS1="\\w\$(__git_ps1 '(%s)') \$ "

# 方法二:
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\n$ "

# 注意事项:
#   1. 不管使用那种方法都要注意“\$”的存在,这用于防止命令被提前执行。否则,当切换到其他目
#      时,分支名不会发生改变;

修改运行进程的输出

redirecting output of running background job in bash
How to redirect output of an already running process

删除指定目录以外的目录

Remove all files except some from a directory
shopt -s extglob
rm !(textfile.txt|backup.tar.gz|script.php|database.sql|info.txt)

相关书籍

《Linux Shell 脚本攻略(第 2 版)》

参考文献

GNU Bash manual:https://www.gnu.org/software/bash/manual/bash.html
怎样写出规范的 shell 脚本(仅限 Scripting with style)
https://www.tutorialspoint.com/unix/case-esac-statement.htm
Bash Reference Manual