0

I have a working bash script that needs to be executed over SSH. My idea is to wrap the whole script into a ssh call. However I run into troubles when I need to run this line:

ssh localhost '''
> set -x
> awk -F 'pattern' '{print $2}' <<< "pattern here"
> '''
+ awk -F pattern '{print' '}'
awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string
'''

Looks like $2 is lost. I have tried to escape it with \ or $$ to no avail. It also looks like the single quotes are lost from pattern, but not from '{print' '}'

I know I could copy the script with SCP and invoke it later with SSH, but I would rather not do that.

Thanks on advance.

  • 1
    `'''` is exactly the same as only one quote, because pairs of quotes cancel each other out (the first quote opens a single-quoted context, the second one ends that context, the third one opens a _new_ single-quoted context; so the net effect is the same as if there had been only one `'`). Shell isn't Python. – Charles Duffy Sep 21 '21 at 17:31
  • 1
    ...in general, a heredoc is the correct tool for this job. `ssh localhost <<'EOF'`, your script, then `EOF`. – Charles Duffy Sep 21 '21 at 17:32
  • Adding to the first [comment](https://stackoverflow.com/questions/69273173/running-bash-script-over-ssh#comment122438665_69273173): The wrong quoting results in just the opposite parts being quoted, i.e. the `$2` is in the unquoted part and gets expanded by the local shell. – Bodo Sep 21 '21 at 17:37

0 Answers0