Bash – How to find the line number in Bash when an error occured

bashlinuxlogsscriptingshell-script

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:

$ cat yael.bash
#!/bin/bash

set -eE -o functrace

file1=f1
file2=f2
file3=f3
file4=f4

failure() {
  local lineno=$1
  local msg=$2
  echo "Failed at $lineno: $msg"
}
trap 'failure ${LINENO} "$BASH_COMMAND"' ERR

cp -- "$file1" "$file2"
cp -- "$file3" "$file4"

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, or f4. When I run the above script:

$ ./yael.bash
cp: cannot stat ‘f1’: No such file or directory
Failed at 17: cp -- "$file1" "$file2"

It fails, reporting the line number plus command that was executed.

Related Question