找到回显出现在bash脚本中的行号

Rad*_*anu 1 bash shell

可以使下面的bash脚本像我在标题中所说的那样工作吗?

#!/bin/bash

echo_report() {
    echo "echo on line $1"
}

trap 'echo_report $LINENO' [sigspec]

#same code here

echo "hi"

#more code here
Run Code Online (Sandbox Code Playgroud)

我不知道应该用什么[sigspec]......

如果trap无法使用,我还有其他选择吗?

Aro*_*fis 5

包装echo函数,然后用于caller显示行号:

#!/bin/bash

echo() {
    caller
    command echo "$@"
}

echo "hi"
Run Code Online (Sandbox Code Playgroud)

结果:

$ bash foo.bash
8 foo.bash
hi
Run Code Online (Sandbox Code Playgroud)