mkdir -p over SSH bash

eekfonky picture eekfonky · Nov 2, 2016 · Viewed 19k times · Source

I have a small test script as follows;

TESTDIR="$HOSTNAME"
ssh [email protected] "\$TESTDIR"
mkdir -p ~/$TESTDIR/test
exit

the output with bash -x is;

+ TESTDIR=ndx
+ ssh [email protected] '$TESTDIR'
+ mkdir -p /home/user/ndx/test
+ exit

Yet on the remote server, no directory exists?

Answer

Jakuje picture Jakuje · Nov 2, 2016

The last argument of ssh is command you want to execute on the remote host:

TESTDIR="$HOSTNAME"
ssh [email protected] "mkdir -p ~/$TESTDIR/test"