0

I am learning awk. I'm confused on how to pass variables to awk in a script. Following the advice at http://www.grymoire.com/Unix/Awk.html I did (note the $ in front of $'$line':

#!/usr/bin/env bash
line=${1:-1}
git status -s | awk 'FNR == $'$line' {print $2}'

After some hair pulling, I got this to work by removing the first dollar sign in front of $line:

#!/usr/bin/env bash
line=${1:-1}
git status -s | awk 'FNR == '$line' {print $2}'

So is the tutorial wrong or is there something about the behavior of variables outside of brackets that is different from those inside?

StevieD
  • 6,925
  • 2
  • 25
  • 45
  • 2
    Take a look at awk's option -v. – Cyrus Mar 30 '19 at 16:11
  • 1
    The tutorial isn't exactly wrong since what you are trying to do is possible given a bunch of caveats but you should only do that in **extremely** rare circumstances. See http://cfajohnson.com/shell/cus-faq-2.html#Q24 for more info. – Ed Morton Mar 30 '19 at 16:33

0 Answers0