0

I am trying to debug a large build project which calls a bunch of bash scripts. Someone suggested modifying PS4 so I set it to the following snippet in order to print the path of the script and the line number being executed.

export PS4='\e[33m+ ${BASH_SOURCE}:${LINENO} \e[0m'

However, when BASH_SOURCE was long, I noticed that all outputs were yellow (because of \e[33m). Upon further examination, I figured out that bash was truncating PS4 whenever it was longer than about 80 chars or so and in such cases \e[0m never got executed. I googled a bit but I didn't find any documentation on this behaviour.

For now my only workaround is to truncate BASH_SOURCE manually. Is there a better way?

export PS4='\e[33m+ ${BASH_SOURCE:${#BASH_SOURCE}<80?0:-80}:${LINENO} \e[0m'
2
  • I can't reproduce the effect you are mentioning, but does wrapping the non-printing escape codes in \[...\] as described in the "PROMPTING" section of the manual help? Also, there is no need to export PS4. Commented Nov 14, 2022 at 6:31
  • Try creating a directory with say 100 chars. I just checked and I can reproduce. Wrapping non-printing escape codes doesn't help. Commented Nov 15, 2022 at 4:32

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.