Include Line Numbers to bash set-x debug output

There are times you need to view the line numbers in a bash script to view script execution.

Within bash ‘set -x’ either at the shell, or within the bash script will display debug output.

To include line numbers you can use the PS4=’${LINENO}:’

The $PS4 variable in Bash is used for the prompt printed before the command line is echoed when the debugging shell option -x is set with the set builtin command

EXAMPLE

#!/bin/bash
PS4='${LINENO}: '
echo "Hello"
echo "World

OUTPUT

# ./test.sh
+ PS4=’${LINENO}: ‘
6: echo Hello
Hello
7: echo World
World
This entry was posted in Command Line FU and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *