Shell终端使用

Shortcuts 快捷键

  • CTRL+N|P 下、上一条命令
  • CTRL+A|E 跳到行首、行尾
  • ALT+F|BCTRL+←|→ 下、上一个单词(右ALT开始使用~)
  • CTRL+W 删除前面的单词
  • CTRL+D|H 删除一个字符(D向后删除=Delete,H向前删除=Backspace)
  • CTRL+U 删除整行
  • CTRL+R 搜索整行,Esc退出

Bash prompt string配置

  • PS(prompt string): 是命令行的默认显示文本,如:
username@hostname /work_directory
$
  • 可以在~/.bash_profile.bashrc中修改默认显示(Windows Git Bash是git-prompt.sh
# Windows Git Bash默认配置,其中\007前面的$TITLEPREFIX和$PWD是标签栏的内容,git_ps1会显示git工作分支
export PS1="\[\e]0;$TITLEPREFIX:$PWD\007\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM \[\e[33m\]\w\[\e[36m\]`__git_ps1`\[\e[0m\]\n$ "
# 显示效果
username@hostname MINGW /work_directory
$

# 简洁配置,将\h换成了指定文本,保留了命令行前的换行,将标签名改为当前目录
export PS1="\[\e]0;\W\007\] \n\[\e[32m\]\u@Host \[\e[33m\]\w\[\e[36m\]`__git_ps1`\[\e[0m\]\n$ "
# 显示效果
username@Host /work_directory
$
  • Windows虚拟环境后不会换行:设置venv/Scripts/activate
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
PS1="\n(${VIRTUAL_ENV_PROMPT}) ${PS1-}" # 添加换行符
export PS1
fi
  • MSYS2显示git prompt
# etc/profile.d/git-prompt.sh
if test -f /etc/profile.d/git-sdk.sh
then
TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
TITLEPREFIX=$MSYSTEM
fi

if test -f ~/.config/git/git-prompt.sh
then
. ~/.config/git/git-prompt.sh
else
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[32m\]' # change to green
PS1="$PS1"'\u@\h ' # user@host<space>
PS1="$PS1"'\[\033[35m\]' # change to purple
PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
PS1="$PS1"'\w' # current working directory
if test -z "$WINELOADERNOEXEC"
then
GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
if test -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
PS1="$PS1"'\[\033[36m\]' # change color to cyan
PS1="$PS1"'`__git_ps1`' # bash function
fi
fi
PS1="$PS1"'\[\033[0m\]' # change color
PS1="$PS1"'\n' # new line
PS1="$PS1"'$ ' # prompt: always $
fi

MSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
for c in "$HOME"/bash_completion.d/*.bash
do
# Handle absence of any scripts (or the folder) gracefully
test ! -f "$c" ||
. "$c"
done
fi

# ~/.bashrc || ~/.bash_profile
shopt -q login_shell || . /etc/profile.d/git-prompt.sh
syntax description
\u username
\h hostname
\e set colors
\w work directory
\W current diretory
\n line feed

Bash Shell

  1. 格式
#!/bin/bash
value="string"(不要有空格)
echo $value

执行使用sh filename.sh
  1. "%d"输出变量, '%d’输出%d(不转义)
  2. declare -afirx 定义变量为数组函数整数只读环境(默认为字符串)
  3. read value 读取变量
  4. $0 $1 $2 表示命令行参数sh file.sh $0 $1
  5. if then
if [ "$yn" = "y" ] && [ 条件二 ](注意空格); then
语句
else
语句
fi 结束
  1. case
case $value in
one)
语句
value)
语句
exit 1
esac
  1. for
for (( i=1;条件2;i=i+1 ))
do
语句
done
  1. while
while [条件] || [条件]
do
语句
done
  1. 调试脚本
sh -nvx
n 检查脚本错误
v 执行脚本,并把脚本内容打印
x 与v稍微不同(折叠显示)

好用的工具

Pandoc

文本格式转换

pandoc --from markdown --to docx source.md -o dest.docx
pandoc -f markdown source.md -t docx -o dest.docx
pandoc source.md -o dest.docx --ignore-args # 忽略参数

注意:为了最佳转换效果,markdown文件每行后都要空行