Bash相关技巧

执行输入命令附带参数

有时候需要使用 curl 命令输出远程脚本给 Bash 执行,如果需要附带参数,可以:

curl http://example.com/script.sh | bash -s -- arg1 arg2

即使 Bash 的输入是用 echo 输出的也可以使用。

命令行文件句柄操作

例如需要读写串口,首先以读写模式打开串口设备,200 为文件句柄号:

~# exec 200<> /dev/ttyUSB2

写入数据:

~# echo -e 'AT+CPIN?\r\n' >&200

读取数据并增加超时:

~# read -t1 TLINE <&200

也可以以读模式打开获取文件句柄:

~# exec 3< echolist

关闭句柄:

~# exec 3<&-

如果以写模式打开句柄,可以这样关闭句柄:

~# exec 3>&-

历史命令

显示历史命令:

~# history -w /dev/stdout

如果需要关闭历史命令功能,可以:

~# export HIST_FILE=/dev/null
~# shopt -u -o history
~# set +o history

手工清除历史命令记录:

~# history -c