0

In c shell script, I'm trying to pass bbb to awk sub, but apparently it does not work.

#!/bin/csh
set aaa=a
set bbb=b

#And I'm using it in awk sub

set ccc=`echo $aaa | awk '{sub("a",$bbb); print $0}'`

echo $ccc

But echos empty and dunno why.

Note if I replace $bbb with "b" then it works well.

cdnszip
  • 253
  • 1
  • 4
  • 9

1 Answers1

1

You have to pass the shell variable to awk. Either with

set ccc=`echo $aaa | awk -v bb=$bbb '{sub("a",bb); print $0}'`

either close and reopen the awk quoting when referring to the shell variable :

set ccc=`echo $aaa | awk '{sub("a",'$bbb'); print $0}'`
magor
  • 3,592
  • 2
  • 11
  • 27
  • Explanation: The shell (`csh`, `bash` etc) does not expand variable when in single tick quotes (`'$this_is_not_expanded'`). – ctrl-alt-delor Jul 18 '16 at 09:28