# help with printf command

**URL:** <https://community.unix.com/t/help-with-printf-command/309708>\
**Category:** Shell Programming and Scripting\
**Created:** [May 6, 2012, 8:39pm UTC](https://community.unix.com/t/help-with-printf-command/309708 "2012-05-06T20:39:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bipbip](https://community.unix.com/letter_avatar/bipbip/32/6_449c533f586939b6e5f246632a46421f.png) [@bipbip](https://community.unix.com/u/bipbip)\
**Post date:** [May 6, 2012, 8:39pm UTC](https://community.unix.com/t/help-with-printf-command/309708/1 "2012-05-06T20:39:23Z")

</div>

hello,

I'm trying to display Unix variable using printf command.

Code:

```nohighlight
 
awk '{ if ( $0 ~ /string/ ) {
printf( "%s\n%s\n",$0,"dsd"); #this one works
printf( "%s\n%s\n",$0,$HOME); #this does not work
} else {
print $0;
}
}' param1_1.txt 

```

I could use here echo command but I'm not sure how to use it properly.

I mean "properly" because the intention of this code is to serach for a particular string in a code and then when the string is found then a specific variable should be displaied in the next line after the string.

with simple text I can achieve this but not with variable.  
Does anyone know how to correct this?

---

<div class="post-metadata">

**Author:** ![neutronscott](https://community.unix.com/user_avatar/community.unix.com/neutronscott/32/2331_2.png) [@neutronscott](https://community.unix.com/u/neutronscott)\
**Post date:** [May 6, 2012, 8:48pm UTC](https://community.unix.com/t/help-with-printf-command/309708/2 "2012-05-06T20:48:07Z")

</div>

You're trying to access a shell variable from awk. You need to pass these to awk with `-v "awkvar=$shellvar"`

so your example would be:

```nohighlight
awk -v "home=$HOME" '
{
  if ( $0 ~ /string/ ) {
    printf( "%s\n%s\n",$0,"dsd"); #this one works
    printf( "%s\n%s\n",$0,home); #this should now work
  } else {
    print $0;
  }
}' param1_1.txt

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [May 6, 2012, 9:23pm UTC](https://community.unix.com/t/help-with-printf-command/309708/3 "2012-05-06T21:23:22Z")

</div>

Is this homework?
