2

Is there a way to redirect my debug output to a file and normal STDOUT on screen.

$ cat test1.sh
#!/bin/bash

echo HAI
echo BYE

$ sh -x test1.sh
+ echo HAI
HAI
+ echo BYE
BYE

I want to redirect the debug output to file and normal output on the screen.

$ sh -x test1.sh >file1
+ echo HAI
+ echo BYE

But I can redirect the output to a file and ending the debug output on screen.

Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27
  • 2
    debug output from `-x` goes to stderr. You can redirect stderr to a file as shown by @Tichodroma – hek2mgl Oct 06 '13 at 12:13
  • What if your script writes to stderr and would like to separate debug output from the output of the script? – pihentagy May 05 '15 at 14:12

3 Answers3

3

Use 2>:

$ sh -x test1.sh 2> /dev/null
HAI
BYE

See https://stackoverflow.com/a/818284/1907906

Community
  • 1
  • 1
2

Default file descriptors on linux system are:

stdin - 0
stdout - 1
stderr - 2

to redirect to file,

sh test1.sh 1> file

to redirect error(stderr) to console(stdout)

sh test1.sh 2>&1
user1502952
  • 1,390
  • 4
  • 13
  • 27
0

I used alias to give the complete command and using the following way

$ alias mytest='sh -x test1.sh 2>out.test'
$ mytest
HAI
BYE
$ cat out.test
+ echo HAI
+ echo BYE

Thanks every one..

Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27
  • Did any of the answers help you so you can accept it? How about upvoting helpful answers? –  Oct 06 '13 at 12:35