-1

I am a bash/shell script noob and trying to do a small script

var=$(someCommand $param1 $param2 | head -2 | tail -1)
echo $var

I am hoping that I will capture the output in var which never works. What could I be doing wrong?

agent.smith
  • 8,664
  • 8
  • 35
  • 46
  • If `someCommand` prints to stderr instead of stdout, you won't capture the output. – Benjamin W. May 24 '22 at 22:11
  • (Which would be [How to store standard error in a variable](https://stackoverflow.com/q/962255/3266847)) – Benjamin W. May 24 '22 at 22:12
  • It could also be a problem with `echo` (see [this Q](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)), but without specifics it's impossible to say. Recommended reading: the [bash tag info](https://stackoverflow.com/tags/bash/info), ["How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask), ["How to create a Minimal, Reproducible Example"](https://stackoverflow.com/help/minimal-reproducible-example), and [this](https://meta.stackoverflow.com/questions/359146) about the importance of exact error messages. – Gordon Davisson May 24 '22 at 22:21
  • Thanks for the answers. I realized that output of `someCommand` goes to stderr – agent.smith May 24 '22 at 22:42

0 Answers0