0

I have a line in my shell script like below

output_logs=`sh script2.sh $1 $2`

script2.sh produce lots of lines of logs. I want to grep specific lines of its output. The problem I have is $output_logs has the entire output of script2.sh as a single line string and grep produces strange results because of this. How to get the logs from script2.sh to be stored in individual lines?

Mohamed Shakeel
  • 361
  • 1
  • 15
  • You aren't showing how you know what `$output_logs` does or doesn't contain -- if you're running `echo $output_logs` and thinking this reflects the variable's output accurately, well, there's your problem (aka [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo)). – Charles Duffy Jun 13 '17 at 01:34
  • I did exactly the same. I did echo `$output_logs`. Fixed it. – Mohamed Shakeel Jun 13 '17 at 01:36

1 Answers1

1

Maybe you should try something like this.

output_logs=`sh script2.sh $1 $2`
echo "$output_logs"

Note that this is different from echo $output_logs.

The double-quoted version of the variable preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas the unquoted version replaces each sequence of one or more blanks, tabs and newlines with a single space.

shady
  • 455
  • 1
  • 7
  • 18
  • `"$1" "$2"`, if we don't want string-splitting there too. (I wonder if the OP *really* wants to pass all parameters, ie. `"$@"`). – Charles Duffy Jun 13 '17 at 01:35