How do you find the line number in Bash where an error occurred?
Example
I create the following simple script with line numbers to explain what we need. The script will copy files from
cp $file1 $file2
cp $file3 $file4
When one of the cp
commands fail then the function will exit with exit 1. We want to add the ability to the function to also print the error with the line number (for example, 8 or 12).
Is this possible?
Sample script
1 #!/bin/bash
2
3
4 function in_case_fail {
5 [[ $1 -ne 0 ]] && echo "fail on $2" && exit 1
6 }
7
8 cp $file1 $file2
9 in_case_fail $? "cp $file1 $file2"
10
11
12 cp $file3 $file4
13 in_case_fail $? "cp $file3 $file4"
14
Best Answer
Rather than use your function, I'd use this method instead:
This works by trapping on ERR and then calling the
failure()
function with the current line number + bash command that was executed.Example
Here I've not taken any care to create the files,
f1
,f2
,f3
, orf4
. When I run the above script:It fails, reporting the line number plus command that was executed.