views:

46

answers:

3

Hi,

this is probably a very stupid question; in a bash script, given the output of, for instance;

awk '{print $7}' temp

it gives 0.54546

I would like to give this to a variable, so I tried:

read ENE <<< $(awk '{print $7}' temp)

but I get

Syntax error: redirection unexpected

Could you tell me why, and what is the easiest way to do this assignment?

Thanks

+2  A: 

You can do command substitution as:

ENE=$(awk '{print $7}' temp)

or

ENE=`awk '{print $7}' temp`

This will assign the value 0.54546 to the variable ENE

codaddict
great, thanks a lot! By the way, anybody knows why the previous one did not wok?
Werner
+1  A: 

your syntax should be

read ENE <<<$(awk '{print $1}' file)

you can directly assign the value as well

ENE=$(awk '{print $7}' temp)

you can also use the shell

$ var=$(< temp)
$ set -- $var
$ echo $7

or you can read it into array

$ declare -a array
$ read -a array <<<$(<file)
$ echo ${array[6]}
ghostdog74
A: 

In general, Bash is kinda sensitive to spaces (requiring them some places, and breaking if they are added to other places,) which in my opinion is too bad. Just remember that there should be no space on either side of an equal sign, there should be no space after a dollar sign, and parentheses should be lined with spaces ( like this ) (not like this.)

`command` and $( command ) are the same thing, but $( this version can be $( nested ) ) whereas "this version can be `embedded in strings.` "

Max E.