Skip to main content
Post Closed as "Duplicate" by muru, Kusalananda awk
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
Source Link
Brian Fitzpatrick
  • 2.9k
  • 6
  • 26
  • 48

How can I prepend the value of a variable to a column in bash?

I have a script that outputs a column of text. I want to prepend each entry in the column with a string stored as a variable. How can I do this?

For example, consider the command

$ echo -e "john\npaul\ngeorge\nringo"
john
paul
george
ringo

Now, I'd like to prepend each entry in this column with the value of the environment variable $HOME. I've tried piping into awk like this:

$ echo -e "john\npaul\ngeorge\nringo" | awk '{printf("%s%s\n", $HOME, $1)}'
johnjohn
paulpaul
georgegeorge
ringoringo

This output is not what I'm looking for. What am I doing wrong?