4

I am trying to print the value of VARI in the same line followed by a comma, so that i can have a csv file of these values, but i m not able to save the value of VARI = 'cat filename | head -1 | cut -d, -f${i}'

i=0
while (( i<130)) ;
do
  if [[ $i -eq 1 ||  $i -eq 9 || $i -eq 12 || $i -eq 23 || $i -eq 25 || $i -eq 29 ]]
  then
    VARI = 'cat filename | head -1 | cut -d, -f${i}'
    echo  "$VARI ,"   
  fi
  let i=$i+1;
done

output expected is

4,abc,5,8,xyz,9

Please let me know what i am doing wrong, thanks!

knittl
  • 246,190
  • 53
  • 318
  • 364
learner
  • 885
  • 3
  • 14
  • 28
  • I don't know how to put back quote. But In `VARI` the line should have back quote – badawi Aug 05 '11 at 15:45
  • @badawi -- tried it, does not work..first.sh: line 8: VARI = cat filename | head -1 | cut -d, -f${i}: command not found – learner Aug 05 '11 at 15:48
  • @badawi: `VARI = \`cat filename | head -1 | cut -d, -f${i}\`` :) – knittl Aug 05 '11 at 15:49
  • @knittl: how did you do that? – badawi Aug 05 '11 at 15:51
  • first.sh: line 8: VARI: command not found.. am i missing something? – learner Aug 05 '11 at 15:54
  • Sorry i just want to rewrite it but i don't know how to put back quote. There is no space around `=`. i.e. `VARI=\`cat filename | head -1 | cut -d, -f${i}\`` – badawi Aug 05 '11 at 15:57
  • Possible duplicate of [How to set a variable equal to the output from a command in Bash?](http://stackoverflow.com/questions/4651437/how-to-set-a-variable-equal-to-the-output-from-a-command-in-bash) – tripleee Nov 02 '16 at 06:03

1 Answers1

11

use backticks (or $() – can be nested), not single quotes:

VARI=`cat filename | head -1 | cut -d, -f${i}` # or:
VARI=$(cat filename | head -1 | cut -d, -f${i})
knittl
  • 246,190
  • 53
  • 318
  • 364