-2

I have a variable in bash RED_FONT='tput setaf 1' and I have a string inside an awk command str="RED_FONT". I want to access RED_FONT variable inside awk. Currently I am passing the bash variable into awk by awk -v RED_FONT=$(echo $RED_FONT)

Rwitaban Goswami
  • 427
  • 3
  • 13
  • Looks good apart from the lack of quotes best I can tell. Did you have a problem you'd like help with? – Ed Morton Aug 11 '19 at 16:12
  • Currently I have RED_FONT='\033[0;31m' inside awk. What I want is to use the variable str="RED_FONT" to access '\033[0;31m' – Rwitaban Goswami Aug 11 '19 at 16:24
  • I have the shell variable inside the awk script. What I can't undertand is how to access the variable given the variable name in a string – Rwitaban Goswami Aug 11 '19 at 16:35
  • Suppose I have the variable abc=4 inside by awk command. I want to access 4 using 'abc', just like I could do it in bash using ${!abc} – Rwitaban Goswami Aug 11 '19 at 16:36
  • 1
    See [GNU Awk User Guide - 6.1 Constants, Variables, and Conversions](https://www.gnu.org/software/gawk/manual/html_node/Values.html#Values) – David C. Rankin Aug 11 '19 at 16:45
  • If I understand your question correctly, you can just say `str=RED_FONT "foo"`in awk to make `str` hold your escape sequence plus the three characters `foo`. You don't use `$` on regular variables in awk, and putting two strings right next to each other joins them together. – cxw Aug 11 '19 at 17:07
  • 2
    Please [edit] your question to provide a [mcve] demonstrating what you're trying to do as the words you're using to describe your needs are changing between the question and your comments and could really mean any of several different things. – Ed Morton Aug 11 '19 at 17:10
  • 3
    `awk -v RED_FONT=$(echo $RED_FONT)` just `awk -v RED_FONT="$RED_FONT"`. And remember about quotes. – KamilCuk Aug 11 '19 at 17:33
  • Awk doesn't have access to shell variables that weren't either `export`ed or passed to it on the command line. If this is just a regular variable, awk has no way to read it. – Charles Duffy Aug 11 '19 at 17:38
  • 1
    (By the way, all-caps names are reserved for variables that modify behavior of the shell or operating system; names you come up with yourself should have at least one lowercase character, per https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) – Charles Duffy Aug 11 '19 at 17:46
  • @EdMorton Look here: https://stackoverflow.com/questions/57448342/sed-inside-awk-with-system-is-not-working/57448776 – hek2mgl Aug 11 '19 at 18:49
  • 2
    @RwitabanGoswami You could have just edited this question ^^^ – hek2mgl Aug 11 '19 at 18:50

1 Answers1

3

If you export your shell variable to the environment, then awk can look it up by name.

Thus:

#!/usr/bin/env bash

set -a ## automatically export variables upon definition
fg_RED=$(tput setaf 1)
fg_WHITE=$(tput setaf white)
set +a ## turn off auto-export

awk '{ print(ENVIRON[$1]); print $2; print(ENVIRON[$3]) }' <<EOF
fg_RED print_me_in_red fg_WHITE
EOF

A shell variable that is neither exported nor passed on awk's command line (as with awk -v varname="$value") is simply not accessible to awk at all.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441