Execute command to remote server via SSH after exporting variable

ddmichael

I have two machines, A and B. I need to execute a script which is located in B by SSHing there from A.

In order for the script to run I also need to export a variable. If I just SSH and export the variable and then try again to SSH and execute the script, the variable is no longer available.

Is there a way to export the variable and make it available in the same session with the script?

I was hoping for something like this (but it doesn't work):

ssh root@B export PATH=$PATH:/add/stuff/here && runScript.sh

Even if I do the following, the path isn't updated:

ssh root@B export PATH=$PATH:/add/stuff/here && echo $PATH
theoden8

You're slightly wrong in passing the third argument. In the command you've provided you pass instructions as many arguments, and, additionally, you echo local $PATH. You must pass the whole command as one argument, in single quotes.

Instead of

ssh root@B export PATH=$PATH:/add/stuff/here && echo $PATH

Which is:

(ssh + root@B + export + PATH=...) && (echo + $PATH)

You should do:

ssh root@B 'export PATH="$PATH:/add/stuff/here" && echo $PATH'

Which is:

ssh + root@B + '(export + PATH=..) && (echo $PATH)'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute command on remote server via ssh

From Dev

Execute inline command remote server with ssh

From Dev

ssh - execute complex remote command

From Dev

Execute a remote script on a remote computer via SSH

From Dev

how to send local variable to remote server via ssh

From Dev

Import remote variable via ssh

From Java

How to execute a remote command over ssh with arguments?

From Dev

ssh config auto execute remote command

From Dev

ssh config auto execute remote command

From Dev

execute remote command over ssh strange behaviour

From Dev

SSH to a server, execute update command that continues even after logging off the ssh session

From Dev

how to execute if else condition with ssh to remote server

From Dev

how to execute if else condition with ssh to remote server

From Dev

R - Connect via ssh and execute a command

From Dev

Execute a sudo NOPASSWD command remotely via SSH

From Dev

SSH into a remote shell, execute a 'source' command, and stay in the remote shell

From Dev

SSH remote command execute stays without disconnect when finished execute?

From Dev

How to run a command automatically everytime I log into a remote server via ssh without disconnecting the session

From Dev

Ansible execute command locally and then on remote server

From Dev

execute awk command with shell variables on remote server

From Dev

execute awk command with shell variables on remote server

From Dev

Command does not execute properly on remote server

From Dev

execute remote command on remote windows via linux while escaping quotes

From Dev

How to remote execute ssh command a sudo command without password

From Dev

Will a query which may take hours to execute keep executing on remote server if I close the connection after executing the command?

From Dev

Clone a remote server to local machine via ssh

From Dev

Rundeck: running command in remote server using ssh

From Dev

SSH to Server, Execute Command, and Maintain Connection

From Dev

SSH to Server, Execute Command, and Maintain Connection

Related Related

  1. 1

    Execute command on remote server via ssh

  2. 2

    Execute inline command remote server with ssh

  3. 3

    ssh - execute complex remote command

  4. 4

    Execute a remote script on a remote computer via SSH

  5. 5

    how to send local variable to remote server via ssh

  6. 6

    Import remote variable via ssh

  7. 7

    How to execute a remote command over ssh with arguments?

  8. 8

    ssh config auto execute remote command

  9. 9

    ssh config auto execute remote command

  10. 10

    execute remote command over ssh strange behaviour

  11. 11

    SSH to a server, execute update command that continues even after logging off the ssh session

  12. 12

    how to execute if else condition with ssh to remote server

  13. 13

    how to execute if else condition with ssh to remote server

  14. 14

    R - Connect via ssh and execute a command

  15. 15

    Execute a sudo NOPASSWD command remotely via SSH

  16. 16

    SSH into a remote shell, execute a 'source' command, and stay in the remote shell

  17. 17

    SSH remote command execute stays without disconnect when finished execute?

  18. 18

    How to run a command automatically everytime I log into a remote server via ssh without disconnecting the session

  19. 19

    Ansible execute command locally and then on remote server

  20. 20

    execute awk command with shell variables on remote server

  21. 21

    execute awk command with shell variables on remote server

  22. 22

    Command does not execute properly on remote server

  23. 23

    execute remote command on remote windows via linux while escaping quotes

  24. 24

    How to remote execute ssh command a sudo command without password

  25. 25

    Will a query which may take hours to execute keep executing on remote server if I close the connection after executing the command?

  26. 26

    Clone a remote server to local machine via ssh

  27. 27

    Rundeck: running command in remote server using ssh

  28. 28

    SSH to Server, Execute Command, and Maintain Connection

  29. 29

    SSH to Server, Execute Command, and Maintain Connection

HotTag

Archive