笔记
replace character
# use “tr” replace ' ' with '/n'
your-shell-command | tr ' ' '\n'`test if a command exist
command -v nvim >/dev/null 2>&1 && export EDITOR="nvim">/dev/null : 将输出重定向到 null
2>&1: 将标准错误重定向到标准输出
if in ssh
[ -n "$SSH_TTY" ] && command -v fish >/dev/null 2>&1 && exec fish后台运行
sleep 100 &-
任务在后台运行,不阻塞当前终端
-
运行后在 C-z 暂停,使用 bg 让暂停的任务在后台继续运行
-
查看后台任务使用jobs(jobs -l)拉回前台使用 fg
-
nohup python train.py > output.log 2>&1 &
-
nohup 表示 no hang up ,常住后台,关闭终端不影响
测试网络
curl https://clients3.google.comthis URL is the best option to ping as it should work 24/7 without maintenance pause
进程替换 Process Substitution
bash/zsh的特性
<(command) # 把 command 的“输出”当成文件
>(command) # 把 command 的“输入”当成文件查看临时文件
ls <(pwd)
# /dev/fd/33执行顺序
diff <(ls) <(ls -a)- Bash 提前 fork 子进程
- 执行 ls
- 把 ls 的 stdout 接到一个 pipe
- 把 pipe 的读端暴露成 /dev/fd/XX
- 把这个路径字符串传给 diff
即首先执行ls,将结果pipe到一个临时文件,接着执行
diff /dev/fd/63 /dev/fd/64>()
# tee 写入两个“文件” 实际是写入两个命令的 stdin
echo "hello" | tee >(grep h) >(wc -c)
# 重定向 stdout 到命令
ls > >(grep py)[ 是test的别名
[并不是 sh 语法,而是一条命令
which [
/bin/[判断一个条件如:
# -a:and, -o:or
[ "$(uname)" = "Darwin" -a -f "/opt/homebrew/bin/brew" ]不能执行命令为条件
# 命令执行失败/成功不能当作条件
[ command -v nvim >/dev/null 2>&1 ]-
- 将stdin当作一个文件
tar -f - </dev/nullbash zsh
zsh把“命令行输入框”当成一个可编程对象(ZLE) bash 只是 readline 的一层绑定