Eric
Eric

Reputation: 185

zsh: escape newline after heredoc in zsh script

I'm trying to write a zsh script that contains a python 1-liner which takes an argument.

#!/bin/zsh

foo_var="foo"

python -c "import sys; print sys.argv" $foo_var

(This isn't my actual code but this is the gist of what I was doing.)

That code outputs the following:

['-c', 'foo']

The one liner got a little longer than I wanted it to, so I put it in a heredoc, like this:

#!/bin/zsh

bar_var="bar"

python << EOF    
import sys                                                                                                                                                    
print sys.argv                                                                                                                                                
EOF                                                                                                                                                           
$bar_var

(Again, not my actual code but same idea.)

which outputs:

['']
./doctest.zsh:14: command not found: bar

I need $bar_var to be on the line as python so it will get passed as an argument, but I can't have anything on the same line as the second 'EOF'. I also can't have anything before the heredoc because python will interpret it as a filename.

Is there a way to work around the mandatory newline after the second EOF, or better yet, is there generally a better way to do this?

(Also this is my first SO post, so please let me know if I've done something wrong in that sense)

Upvotes: 1

Views: 779

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

This might do what you want:

python - $bar_var << EOF 
import sys
print sys.argv
EOF

Upvotes: 1

Related Questions