Equivalent of __FILE__ and __LINE__ in Bash

#!/bin/bash

echo $LINENO
echo `basename $0`

$LINENO for the current line number
$0 for the current file. I used basename to ensure you only get the file name and not the path.

UPDATE:

#!/bin/bash

MY_NAME=`basename $0`

function ouch {
   echo "Fail @ [${MY_NAME}:${1}]"
   exit 1
}

ouch $LINENO

You have to pass the line as a parameter if you use the function approach else you will get the line of the function definition.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)