2

I can use <(some command) in place of a file, but how can I achieve the equivalent with a variable? I'm using the paste command, which expects files as arguments, and want to use the contents of SOMEVAR as though it were a file.

paste <($SOMEVAR)

Update0

I'd expect something like <<<(SOMEVAR) to exist. <(echo $SOMEVAR) doesn't work, because echo inserts spaces between arguments, so newlines are lost.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526

2 Answers2

3

You could try to use echo with quotes around the $var as that should preserve the newlines, like this

paste <(echo "$SOMEVAR")
Soren
  • 14,402
  • 4
  • 41
  • 67
2

Use a here string for that. Like this:

cmd <<< "$variable"

Example:

file1:

Linux
Unix
Solaris
HPUX
AIX

Bash:

a=$(<file1)
paste <<< "$a" # Will output the contents of file1
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • My concern with this is it ties up use of stdin. I want to pass the variable as one of the file arguments. – Matt Joiner Mar 16 '16 at 20:34
  • @MattJoiner Ok, I missed that requirement. Then go with Soren's answer! That's what you want. A process substitution will get expanded by bash to a file name like `/dev/fd/XY` . Therefore you can even pass multiple of them in a command line and it will not use stdin. – hek2mgl Mar 16 '16 at 20:43