Ali
Ali

Reputation: 267049

How to cd on remote server via SSH bash script?

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?

Upvotes: 2

Views: 17952

Answers (4)

Rudra
Rudra

Reputation: 739

You can try this :

ssh abc@hostname :/pathto/specific directory

Upvotes: 0

Antarus
Antarus

Reputation: 1621

You can use the following command

ssh user@watevr <the_cmd_to_be_executed>

Upvotes: 0

rtytgat
rtytgat

Reputation: 497

Normally you'd just edit your ~/.profile on the remote machine.

If that is not an option, you could do something like this:

ssh -t theserver.com 'cd /some/dir && bash -i'

Upvotes: 6

KBart
KBart

Reputation: 1598

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

Upvotes: 10

Related Questions