Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh server bash -c "cd /tmp && git pull" , cd does not work, need to add echo first

Tags:

bash

ssh

I'm on ubuntu 15.04, my version of ssh client is

OpenSSH_6.9p1 Ubuntu-2ubuntu0.2, OpenSSL 1.0.2d 9 Jul 2015

When I try to run the following command ssh admin@server bash -c 'cd /path/to/repo && git pull' the cd is not effective and i got

fatal: Not a git repository (or any of the parent directories): .git

However If I do

ssh admin@server bash -c 'echo test && cd /path/to/repo && git pull'

then it works

Already up-to-date.

Of course I'm well aware echo is not supposed to change anything but after trying several time, several days on several different servers (though all on debian) I'm now sure to have this error. On other servers I tried the command cd /tmp && pwd , and I got my home directory, and if i do echo toto && /tmp && pwd I go /tmp printed...

like image 756
allan.simon Avatar asked Jul 06 '26 05:07

allan.simon


1 Answers

Unfortunately, ssh passes through a single command line string to $SHELL -c on the remote. Your quotes aren't being effective.

When you run

ssh admin@server bash -c 'cd /path/to/repo && git pull'

this is being run on the remote server (with $SHELL -c):

bash -c cd /path/to/repo && git pull

So Bash is given single command (cd) and an unused argument, and then separately, you're also running git pull in the home directory.

On the other hand, when you run

ssh admin@server bash -c 'echo test && cd /path/to/repo && git pull'

this is being run on the remote server:

bash -c echo test && cd /path/to/repo && git pull

The first part is again useless, but the shell running the whole command then does cd /path/to/repo and git pull. Which works.

What you probably want to do is

ssh admin@server 'cd /path/to/repo && git pull'
like image 172
ephemient Avatar answered Jul 08 '26 14:07

ephemient



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!