I
I
ilovemaryjane2018-11-08 00:22:39
linux
ilovemaryjane, 2018-11-08 00:22:39

Why doesn't the variable work in ssh inside a bash script?

I'm trying to create a file on the server using data from a variable

#!/bin/bash

nginx_default_page="server {
  listen 80;
  listen [::]:80;

  location / {
    proxy_pass http://127.0.0.1:3000;
  }

  error_page 500 502 503 504 /custom_50x;

  location = /custom_50x {
    root /usr/share/nginx/html;
    default_type application/json;
    internal;
  }
}
"

ssh $user@$host << 'ENDSSH'
echo "$nginx_default_page" | sudo tee /etc/nginx/conf.d/default.conf
ENDSSH

The variable is in quotes to preserve the formatting (otherwise everything is in a string). Is it possible in another way, or does it not affect?
An empty file is created default.conf. Why, how to fix?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2018-11-08
@ilovemaryjane

VAR="blablabla
blablabla"

echo "$VAR" | ssh $user@$host 'cat>/tmp/file'
ssh $user@$host "sudo cp /tmp/file /etc;rm /tmp/file"

P
planc, 2018-11-08
@planc

something like:
ssh [email protected]$host bash -c 'echo $моя_переменная'

J
jcmvbkbc, 2018-11-08
@jcmvbkbc

Why, how to fix?

Because only selected environment variables are passed to the remote machine in an ssh session.
Copy a complex script in its entirety - it will be easier to debug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question