So I have this here:

echo "$(some docker command that gives output)" > file

When I run this from Jenkins, the command’s output is shown in the Jenkins log. I do not want this to happen.

How can I grab the output from the command and silently redirect it to a file?

EDIT: I figured out what the issue was. Was using -ex for bash options without understanding what they were. Removed them and am now receiving expected behavior.

  • 1
    Your commands do redirect to file, however it only does it for stdout, what you get in jenkins log may be stderr output which is not redirected in your case. To check that, add 2>&1 at the very end of your command – Tagwint Jul 6 '18 at 15:22
  • 1
    Why echo and $()? – ctrl-alt-delor Jul 6 '18 at 17:04
  • @ctrl-alt-delor cause I’m just dumb. I got rid of that too. – Mike Jul 6 '18 at 17:06
  • @mike, we are all stupid, some times. – ctrl-alt-delor Jul 6 '18 at 17:08

Like @Tagwint said in the comment above, you might need to redirect stderr as well. Also, you don't need echo $(...), you can just use ...:

someDockerCommand > file 2>&1

Assuming that you're using bash, that can be simplified:

someDockerCommand &> file

Both of those redirect both standard output and standard error to the specified file.

  • I’ve just tried to &> and the same output in the Jenkins log is shown. It’s like Jenkins just parses everything and spits it out into the log. – Mike Jul 6 '18 at 15:39
  • 2
    @Mike: &> was added in bash v4 if I'm not mistaken, it may not be available in your bash (especially if you are using mac). It also wont be available in sh. – Jesse_b Jul 6 '18 at 15:42
  • @Jesse_b ahh. That explains it. I’m using sh on the container in question. – Mike Jul 6 '18 at 15:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.