0

I want to execute following line in remote server

export EXTERNAL_IP=`hostname -I | awk '{print $1}'` && echo $EXTERNAL_IP

which fill EXTERNAL_IP on the remote host.

How should I write this line using ssh MyServer MyCommand ?

  • `ssh MyServer "hostname -I | awk '{print $1}'"`? – Cyrus Dec 06 '21 at 22:03
  • @Cyrus point is to fill EXTERNAL_IP – Morteza Malekabadi Dec 06 '21 at 22:08
  • @MortezaMalekabadi just use: ssh myserver "export EXTERNAL_IP=`hostname -I | awk '{print $1}' && echo Its: $EXTERNAL_IP"` if you want to use EXTERNAL_IP – Edgar Magallon Dec 06 '21 at 22:13
  • 1
    The syntax for executing commands via ssh, is `ssh yourserver commands`. Where the commands (if you want to execute two or more) must be with " " or ' ' (there are differences between using single quotes and double quotes – Edgar Magallon Dec 06 '21 at 22:20
  • 1
    You can consider using `\$` when you do `echo $EXTERNAL_IP`, for example `echo \$EXTERNAL_IP`. That because may exist a variable called EXTERNAL_IP in your current shell session or, unless in zsh, without `\$` will not show anything when run the ssh commands. – Edgar Magallon Dec 06 '21 at 22:26
  • 2
    What are you hoping to accomplish by temporarily setting a variable on a remote machine? – Ed Morton Dec 07 '21 at 00:10
  • @MortezaMalekabadi, environment variables only live as long as the processes they're set in do. The moment `ssh` exits the variable is gone, so what's the point? – Charles Duffy Dec 07 '21 at 00:58
  • 1
    Mind, for backticks &c., the common practice is just to write a shell function and tell your shell to expand it into the ssh command line; that way all the escaping is done for you automatically. The process is described in [Shell script: Run function from script over ssh](https://stackoverflow.com/questions/22107610), and if you don't have any problem _not_ solved by following that practice, I'd be tempted to mark this question as a duplicate. (It's not obvious, but that's the point of keeping duplicates in our knowledgebase, to act as signposts) – Charles Duffy Dec 07 '21 at 00:59
  • 1
    ```myfunc() { export EXTERNAL_IP=`hostname -I | awk '{print $1}'` && echo $EXTERNAL_IP; }; ssh MyServer "$(declare -f myfunc); myfunc"``` – Charles Duffy Dec 07 '21 at 01:04
  • 1
    ...mind, you shouldn't be using backticks in modern shell scripts at all. `$( ... )` is the modern replacement, standardized by the publication of POSIX.2 in the early 1990s. It's not just more visually obvious, it also reduces bugs because it doesn't change how backticks and backslashes are parsed inside command substitutions; nesting backticks is a royal pain. – Charles Duffy Dec 07 '21 at 01:05
  • @CharlesDuffy Thank you so mcuh, myfunc() { export EXTERNAL_IP=`hostname -I | awk '{print $1}'` && echo $EXTERNAL_IP; }; ssh MyServer "$(declare -f myfunc); myfunc" works perfectly. I would be grateful if you could post this as an answer. – Morteza Malekabadi Dec 07 '21 at 14:47
  • @EdMorton I want to do some automation with gitlab-ci I need to fill varibale to use them in docker compose. – Morteza Malekabadi Dec 07 '21 at 14:48
  • @EdgarMagallon Thanks Edgar that was really good insight – Morteza Malekabadi Dec 07 '21 at 14:49

0 Answers0