I am trying to execute a script on remote servers, passing the script as the last argument

ntrs exec-all-ubuntu --exec `cat << 'EOF'

  echo "$(pwd)"
  echo "$foobar"


The problem is that the values in the text are sent as separate arguments, echo is the first arg and the pwd value is a second separate arg, but I want just one argument as a string

The arguments end up looking like this:

[ '--exec', 'echo', '"$(pwd)"', 'echo', '"$foobar"' ]

but I am looking for something literal with newlines:

[ '--exec', '   echo "$(pwd)"\n\n echo "$foobar"\n ' ]

I also tried using this:

ntrs exec-all-ubuntu --exec `read -d << EOF
    select c1, c2 from foo
    where c1='something'

but that string is empty

3 Answers 3


You can simply use a regular string with embedded newlines:

ntrs exec-all-ubuntu --exec '
  echo "$(pwd)"
  echo "$foobar"
  • thanks that's very good! appreciate it Commented May 14, 2019 at 18:41

From the manual page for bash(1):

The format of here-documents is:


No parameter and variable expansion, command substitution, arithmetic
expansion, or pathname expansion is performed on word.  If any part of
word is quoted, the delimiter is the result of quote removal on word,
and the lines in the here-document are not expanded.

Given that your post is tagged I'd suggest:

ntrs exec-all-ubuntu --exec "$(cat << 'EOF'

  echo "$(pwd)"
  echo "$foobar"



echo "$(pwd)"

might be better as simply:


Jim L. is right, but here's an even less verbose way?

ntrs exec-all-ubuntu --exec "`cat << 'EOF'

  echo "$(pwd)"

  echo "$foobar"


the double quotes around the bacticks are the way to go?

  • 1
    The only difference is that this uses backticks instead of $(), no? Commented May 14, 2019 at 18:16
  • yeah pretty much, I wish there was an easier way to do this, maybe by avoid cat altogether? can't we just use < EOF or something and drop cat? Commented May 14, 2019 at 18:18
  • It look like --exec expects a string as its argument, can't you just use --exec "echo \"$PWD\"; echo \"$foobar\""? Commented May 14, 2019 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .