bash debug line number

当遇到很长 bash 脚本,尤其包含很多逻辑的时候,需要用 bash 行号进行debug,方法是设置 PS4 变量。

[vagrant@centos7-work ~]$ sh foobar.sh 
foobar
[vagrant@centos7-work ~]$ export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ '
[vagrant@centos7-work ~]$ sh -x foobar.sh 
(foobar.sh:2) $ echo foobar
foobar

参考:
https://coderwall.com/p/wil8da/debug-bash-scripts-with-line-numbers-more-info