I have this working command in the terminal (bare in mind the ssh to localhost is for testing - makes no difference if it's a remote server or not):

> ssh nick@ sh -s -- < "./a.sh" < "./b.sh"


export var="hey world"


echo "$var"

prints "hey world" as expected.

But when I add the same command into a script file as such (let's say it's named run.sh):


# ...
ssh nick@ sh -s -- < "./a.sh" < "./b.sh"

and I execute as follows:

chmod +x ./run.sh

it doesn't work - a blank line is printed. I've tested other commands and they similarly do not appear to have been read.

What's going on?

EDIT: I sohuld have clarified, I have solved the issue in itself with the same answer Cornholio came to, but I want to know precisely what is causing it to work via REPL vs in a shell script file


You're probably using a shell different from /bin/sh for your command line test. Your problem is redirection support in /bin/sh. I've found no way to append the contents of two files to stdin. To see what's happening you can replace the ssh command with cat:

> cat < "./a.sh" < "./b.sh"
echo "$var"

> cat < "./a.sh" 
export var="hey world"

> cat < "./b.sh" 
echo "$var"

How about cat a.sh b.sh ... | ssh nick@

| improve this answer | |
  • I should have mentioned that I did use this exact cat solution! But I'm more interested in why it isn't working... – Nick Bull Feb 1 '19 at 15:32
  • Good job as a solution the problem though! Have a +1 :) – Nick Bull Feb 1 '19 at 15:34
  • 1
    I'd say you're using different shells ... not sure which is your command line but the script is /bin/sh. My bash also has problems with multiple redirects. – Cornholio Feb 1 '19 at 16:11
  • Ah, you're right. I'm using zsh :) Add that to the answer and I'll accept! – Nick Bull Feb 1 '19 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.