1

Why would this work at the command line:

redis-cli info | grep instantaneous_ops_per_sec | cut -d : -f 2

...but not work when I assign that to a variable inside a shell script? e.g.,

num=`redis-cli info` | grep instantaneous_ops_per_sec | cut -d : -f 2

Relatedly, why does assigning the raw output of redis-cli info to a shell variable and then echoing it result in a garbled version of the redis-cli output?

rivenmyst137
  • 345
  • 1
  • 4
  • 10

1 Answers1

0

When capturing to a variable you should put the entire chain of commands in a subshell, not just the beginning portion:

num=$( redis-cli info | grep instantaneous_ops_per_sec | cut -d : -f 2 )

Also it's better to use $( ) rather than legacy backticks.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 1
    I swear I had tried that. :-/ Why is it that 'cat'-ing a file into a shell variable and then echoing that file results in a garbled version of that file? – rivenmyst137 Nov 18 '16 at 03:26
  • @rivenmyst137 Sounds like [this issue](http://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – that other guy Nov 18 '16 at 04:36