Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cd on remote server via SSH bash script?

Tags:

linux

bash

unix

ssh

I have a bash script I'm using to connect to a remote server via ssh. That works, however I want the script to immediately pass the command of cd /some/dir after connecting. That doesn't seem to be working. Here's my code:

#!/bin/bash
echo "SSHing.."
ssh -i ~/.ssh/some-site.pem [email protected]
cd /some/dir        
read

How can I have the cd command be executed right after SSH connection is established?

like image 699
Ali Avatar asked May 17 '26 01:05

Ali


1 Answers

There are two easy ways to execute commands via SSH from inside the script:

1) ssh user@host 'command'

2)

ssh user@host <<<EOF
command1
command2
<...>
commandn
EOF
like image 144
KBart Avatar answered May 18 '26 17:05

KBart



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!