开发者

bash: showing executed commands expect echos

开发者 https://www.devze.com 2023-03-04 17:28 出处:网络
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:

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:

$ 开发者_如何学Cbash -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


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消