Max
Max

Reputation: 13334

bash: showing executed commands expect echos

I like using the -x switch with bash to debug scripts. The only downside is that the echo commands are also display which may create a lot of unnecessary duplicates:

#!/bin/bash
echo "Changing to /etc directory"
cd /etc

Then once run:

$ bash -x test.sh 
+ echo 'Changing to /etc directory'
Changing to /etc directory
+ cd /etc

I tried filtering out with bash -x test.sh | grep -v '+ echo' but it doesn't work.

[Q] Is there a way I can prevent -x to also display echo commands?

Thanks

Upvotes: 1

Views: 291

Answers (1)

sapht
sapht

Reputation: 2829

You could filter it through a regex engine of your choice:

$ ./test.sh 2>&1 | awk '{if(!match($0, /^\+ echo/)){print $0}}'

or

$ ./test.sh 2>&1 | grep -v '^\+ echo'

Upvotes: 2

Related Questions