1

This question is motivated by Jenkins jobs, and their Execute shell build step. Jenkins by default calls sh with -x switch, which results in echoing the commands it executes. This is definitely good and desired behaviour. However, it would be very nice to be able to just print messages nicely, in addition to having the set -x in effect. An example follows:

If there is echo Next we fix a SNAFU in the build step script, console output of the build will have

+ echo Next we fix a SNAFU
Next we fix a SNAFU

It would be much nicer to have just single line,

Next we fix a SNAFU

How to achieve this? Solution is ideally general sh solution, but Jenkins-specific solution is fine too. And solution should be quite nice looking in the shell script source too, as the dual purpose of the echoes is to both document the script, and make the output more clear.


So just surrounding every echo like that with

set +x
echo Message
set -x

is not very good, because it will print + set +x before every message, and it also takes up 3 lines in the script.

hyde
  • 50,653
  • 19
  • 110
  • 158

2 Answers2

1

set +x

<command>

set -x

This will disable the printing of the <command>

Zloj
  • 2,040
  • 2
  • 16
  • 27
0

I found two solutions, neither is ideal but both are kind of ok.


I like better this first one. Instead of having echo Message, have

true Message

It will display

+ true Message

This works, because true ignores its command line arguments (mostly). Downside is the + true clutter before message, and possibly confusing use of true command for others who read the script later. Also, if echo is turned off, then this will not display the message.


Another way is to do echo like this:

( set +x;echo Message )

which will print

+ set +x
Message

This works, because commands in () are executed in a subshell, so changes like set don't affect the parent shell. Downside of this is, it makes the script a bit ugly and more redious to write, and there's an extra line of output. Also, this spawns an extra subshell, which might affect build performance slightly (especially if building under Cygwin on Windows). Positive is, it will also print message if echo is off, and perhaps intention is immediately clear to those who know shell scripting.

Community
  • 1
  • 1
hyde
  • 50,653
  • 19
  • 110
  • 158