You are not logged in.

#1 2021-02-07 07:18:28

cdwijs
Member
Registered: 2010-04-24
Posts: 294

[SOLVED] Expanding variables in a ssh command?

Hi all,

I'm trying to execute commands on another server via ssh in a script. This works, I get a listing of the /boot directory of the slave server:

#!/bin/bash
bootdiratslave=$(ssh 192.168.31.106 -i ~/slaveroot 'ls /boot/')
echo $bootdiratslave

This does not work, I get a listing of the / directory of the slave server:

#!/bin/bash
slave_dir="/boot"
bootdiratslave=$(ssh 192.168.31.106 -i ~/slaveroot 'ls /$slave_dir')
echo $bootdiratslave

I think the variable slave_dir is not known at the slave server, so /$slave_dir expands to /

how can I use variables in ssh statements?

Last edited by cdwijs (2021-02-07 07:35:54)

Offline

#2 2021-02-07 07:25:29

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 681

Re: [SOLVED] Expanding variables in a ssh command?

Use soft (i.e. double) quotes for that ls rather than hard (i.e. single) quotes.

Note this is not a question relating to ssh etc. The shell simply won't expand anything within hard quotes.

Last edited by bulletmark (2021-02-07 07:27:23)

Offline

#3 2021-02-07 07:35:06

cdwijs
Member
Registered: 2010-04-24
Posts: 294

Re: [SOLVED] Expanding variables in a ssh command?

It works, thank you for your fast answer.
My apologies for posting this in the wrong section.

Offline

Board footer

Powered by FluxBB