# Awk variable replacement

**URL:** <https://community.unix.com/t/awk-variable-replacement/175064>\
**Category:** Shell Programming and Scripting\
**Created:** [August 30, 2007, 7:09am UTC](https://community.unix.com/t/awk-variable-replacement/175064 "2007-08-30T07:09:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iAm4Free](https://community.unix.com/letter_avatar/iam4free/32/6_449c533f586939b6e5f246632a46421f.png) [@iAm4Free](https://community.unix.com/u/iAm4Free)\
**Post date:** [August 30, 2007, 7:09am UTC](https://community.unix.com/t/awk-variable-replacement/175064/1 "2007-08-30T07:09:15Z")

</div>

I have a function

awkvarrep() {  
awk -F'|' '$1~/$1/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile  
}

I'm calling it by this  
VARREP=XYZ  
awkvarrep $VARREP

since i'm passing $VARREP to the awkvarrep() function I want to use this with $1 but it dosen't seem to be working inside the awk in the Shell Script. Do I need to use an escape sequence for it to work pls advice or else I need to do this like 10 times!

---

<div class="post-metadata">

**Author:** ![Klashxx](https://community.unix.com/user_avatar/community.unix.com/klashxx/32/326_2.png) [@Klashxx](https://community.unix.com/u/Klashxx)\
**Post date:** [August 30, 2007, 8:48am UTC](https://community.unix.com/t/awk-variable-replacement/175064/2 "2007-08-30T08:48:09Z")

</div>

Try :

```nohighlight
awkvarrep() {
awk -F'|' '$1~/'"$1"'/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
}

```

---

<div class="post-metadata">

**Author:** ![iAm4Free](https://community.unix.com/letter_avatar/iam4free/32/6_449c533f586939b6e5f246632a46421f.png) [@iAm4Free](https://community.unix.com/u/iAm4Free)\
**Post date:** [August 30, 2007, 9:36am UTC](https://community.unix.com/t/awk-variable-replacement/175064/3 "2007-08-30T09:36:50Z")

</div>

> [@klashxx](#):
>
> Try :
> 
> ```plaintext
> awkvarrep() {
> awk -F'|' '$1~/'"$1"'/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
> }
> 
> ```

Thanks !!! Way to go it works 🙂

---

<div class="post-metadata">

**Author:** ![iAm4Free](https://community.unix.com/letter_avatar/iam4free/32/6_449c533f586939b6e5f246632a46421f.png) [@iAm4Free](https://community.unix.com/u/iAm4Free)\
**Post date:** [August 30, 2007, 9:48am UTC](https://community.unix.com/t/awk-variable-replacement/175064/4 "2007-08-30T09:48:06Z")

</div>

> [@klashxx](#):
>
> Try :
> 
> ```plaintext
> awkvarrep() {
> awk -F'|' '$1~/'"$1"'/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
> }
> 
> ```

Can you explain why whe need to put it in double quotes pls.

---

<div class="post-metadata">

**Author:** ![robotronic](https://community.unix.com/user_avatar/community.unix.com/robotronic/32/57_2.png) [@robotronic](https://community.unix.com/u/robotronic)\
**Post date:** [August 30, 2007, 11:00am UTC](https://community.unix.com/t/awk-variable-replacement/175064/5 "2007-08-30T11:00:23Z")

</div>

Actually, the single quotes are making the difference, not the double ones:

```nohighlight
awk -F'|' '$1~/'"$1"'/{ code... }'

```

All the awk code is enclosed in single quotes, when you want to "break" the awk code because you need the value held by an external shell environment variable, you simply close your code with a quote and reopen it (with another quote) when you've finished the external variable evaluation.

Putting double quotes around a shell variable is only a good scripting practice (so you avoid possible problem with null values, etc...).

---

<div class="post-metadata">

**Author:** ![iAm4Free](https://community.unix.com/letter_avatar/iam4free/32/6_449c533f586939b6e5f246632a46421f.png) [@iAm4Free](https://community.unix.com/u/iAm4Free)\
**Post date:** [August 30, 2007, 11:14am UTC](https://community.unix.com/t/awk-variable-replacement/175064/6 "2007-08-30T11:14:39Z")

</div>

> [@robotronic](#):
>
> Actually, the single quotes are making the difference, not the double ones:
> 
> ```plaintext
> awk -F'|' '$1~/'"$1"'/{ code... }'
> 
> ```
> 
> All the awk code is enclosed in single quotes, when you want to "break" the awk code because you need the value held by an external shell environment variable, you simply close your code with a quote and reopen it (with another quote) when you've finished the external variable evaluation.
> 
> Putting double quotes around a shell variable is only a good scripting practice (so you avoid possible problem with null values, etc...).

Thanks its very clear explanation!
