SSH – Difference Between Running Command in SSH Shell Manually vs SSH User@Host

bashssh

I was assuming it's the same, but it's not.

In my particular case a build script is working fine when executed as ./build.sh from ssh session on the build server like this

$ssh user@host
$./build.sh

but fails when ran as an ssh argument like this:

$ssh user@host /home/user/build.sh

or like this

$ssh user@host ./build.sh

The script itself just calls a bunch of binaries and their error messages are not very helpful for debugging.

But it shouldn't matter what is in the script, as long as the environment is the same, right? So what may be the difference?

Edit:
Don't know if it matters, but the described behavior happens on an ArchLinux Virtualbox guest without a desktop environment. I also tried the same on a Ubuntu18.04 VM with a desktop environment and the script worked correctly in both cases.

Best Answer

I think I figured it out.

The difference between the two cases is interactive vs non-interactive shell, which are indeed significantly different environments.

Instead of running the script as $ssh user@host ./build.sh, what worked was opening a login shell first by calling bash -l, like this:

$ssh user@host bash -l ./build.sh

I don't know if it makes the execution environment exactly the same as running the script manually via ssh, but it fixed the error in my case.