Search code examples
linuxbashshellsedrhel

How to fetch output of command into a variable?


I am trying to run a command and storing the values in a list

list = `sed -n 's/^abc//p' /etc/filename`

I am getting an error command not found while running the above command.

However, when I directly run the sed -n 's/^abc//p' /etc/filename command, the output is coming fine as below:

abc01 abc02 abc03

Solution

  • Use

    list="$(sed -n 's/^abc//p' /etc/filename)"
    

    There must be no spaces after variable declaration and equals sign. Also, quoting your variables is important.