0

how do i assign the output of command to another variable in shell, here what i tried.

summary = `grep -A 4 '#HM based Segregation FEP (WNS)(TNS)' some_file | awk '{print $2, $3}' | sed 's/,=,//g' | tr '\n' ',' | sed 's/based Segregation,//g' | sed 's/(\|\ (/\//g' | sed 's/)//g'`
summary1 = $(grep -A 4 '#HM based Segregation FEP (WNS)(TNS)' some_file | awk '{print $2, $3}' | sed 's/,=,//g' | tr '\n' ',' | sed 's/based Segregation,//g' | sed 's/(\|\ (/\//g' | sed 's/)//g')
echo "$summary"
echo "$summary1"

basically, I am grepping from some patterns and removing unwanted data. and the output is

summary: Command not found.
Illegal variable name.

what am I doing wrong?

swarak
  • 89
  • 5
  • A variable may be assigned to by a statement of the form `name=[value]`, with no space before and after the equal sign. https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html#Shell-Parameters – Philippe May 02 '21 at 08:00

1 Answers1

1

Shell variable assignment syntax is var=value. Remove the spaces around the equal. For example:

summary="$(grep -A 4 '#HM based Segregation FEP (WNS)(TNS)' some_file | awk '{print $2, $3}' | sed 's/,=,//g' | tr '\n' ',' | sed 's/based Segregation,//g' | sed 's/(\|\ (/\//g' | sed 's/)//g')"
loleary
  • 11
  • 1
  • Duplicates should be closed, not answered. See the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy May 03 '21 at 01:22