I'm pretty new to shell/bash scripting and I'm writing a script on OSX to automate some rather tedious efforts. I'm trying to run a portion of my script on a remote host using ssh. There's a common script on both machines, which I'll call ascript. On both machines, this script is in /usr/local/bin.

I know that I need the full path to ascript on my remote host to run it like so:

ssh user@otherHost bash -c "/usr/local/bin/ascript --FlagToDoSomething"

However, I've found that --FlagToDoSomething only runs correctly if I write it the following way in my script:

ssh user@otherHost bash -c " /usr/local/bin/ascript --FlagToDoSomething"

(ie it needs a new line after the open quotes) I get a message in the Terminal window saying bash: -c: option requires an argument, but the rest of it continues to run and I get the output that I'm expecting. If I don't do that, I just get the output as though I just typed in ascript and hit enter (so the usage info).

If I remove bash -c it works correctly, but I'm only doing it this way because, for whatever reason: ssh user@otherHost "output=$(/usr/local/bin/ascript --FlagToDoSomething; echo \$output)" attempts to run this function on the local host, which causes some funky output. And I need to run multiple commands on the remote host before terminating my ssh connection.


2 Answers 2

ssh user@otherHost "output=$(/usr/local/bin/ascript --FlagToDoSomething; echo \$output)"

What the heck is that supposed to do? $output isn't set yet, since the echo is inside the $(command substitution).

Also, $() is expanded by the shell inside double quotes. That's why it runs locally. Use single quotes to avoid it.

Perhaps you want

output=$(ssh u@oh '/usr/local/bin/ascript --FlagToDoSomething')
  • What the heck it's supposed to do is exactly what I'm getting with single quotes! Duh, that was stupid of me. Essentially, I wanted the output of "--FlagToDoSomething" to be echo'd. Using double quotes wasn't doing that for me, so I tried to figure out some way of using echo to do that. Clearly, that compounded issues that I didn't intend for. Thanks!
    – bocks
    Commented Aug 11, 2015 at 22:39
  • Oh, well the ")" is just in the wrong spot actually. Should be right after --FlagToDoSomething. Regardless, using single quotes seems like a less verbose (and potentially screwy) way of accomplishing that.
    – bocks
    Commented Aug 11, 2015 at 22:41
  • @bocks: The ) in the wrong spot was my point. The echo was inside the command substitution! Note that using echo $output instead of echo "$output" collapses all whitespace (including newlines) in the output of ascript into single spaces. Is that what you're trying to do? Otherwise you don't need the capture/echo at all. If it is what you want, echo $(command) should do the same thing. Commented Aug 11, 2015 at 22:44
  • Ah, all of this stuff is super useful Peter. Thanks! And yup, will be accepting now. Just wanted to make sure it worked correctly on my end before making this answer green.
    – bocks
    Commented Aug 11, 2015 at 23:02
  • @bocks: cool. I wouldn't normally bug people so quickly about accepting, just that I wanted the 200-daily-rep badge, and it's only 1hr to the end of the day (UTC). Had two oddly-high-scoring answers racking up rep for me today. :P Commented Aug 11, 2015 at 23:09

Remove "-c" and put the whole shebang in quotes:

output=$(ssh [email protected] "bash /usr/local/bin/ascript --testable=fly"); 

echo $output;
  • Am I supposed to leave the bash in quotes?
    – bocks
    Commented Aug 11, 2015 at 22:25
  • you're welcome to play with the quotes... but it works for me exactly as written ARGES GIVEN: --testable=fly Commented Aug 11, 2015 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .