LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   storing the value of echo in a variable (https://www.linuxquestions.org/questions/linux-newbie-8/storing-the-value-of-echo-in-a-variable-4175493099/)

jone kim 01-30-2014 03:15 AM

storing the value of echo in a variable
 
Code:

#!/bin/bash

for i in `SOME LINUX COMMAND`
        do
                echo "$i"
        done

how to store the value the value of i in another variable. when i did this I get the following error:
Code:

#!/bin/bash

for i in `SOME LINUX COMMAND`
        do
                j = $(echo $i)
                echo "$j"
        done


I am getting the error "j command not found" .

why is this error coming? How to solve this?

markush 01-30-2014 03:47 AM

There must not be spaces at both sides of the '=', try
Code:

j=$(echo $i)
But also
Code:

j=$i
should work.

Also you must not put the text between 's, write
Code:

for i in SOME LINUX COMMAND
Markus

jlinkels 01-30-2014 05:50 AM

But the OP wrote `SOME LINUX COMMAND` (note the backticks)
It seems that he want to use the output of a command, by iterating through the output. Therefore the output is assigned to $i.

I recommend not to use backticks but $(SOME LINUX COMMAND) instead in all cases where the output of a command is needed. For the sake of readability, backticks are confusing and can easily be overlooked.

jlinkels


All times are GMT -5. The time now is 03:01 PM.