Linux – Executing local bash script remotely, interactively, with arguments

bashlinuxshellssh

From all of my testing it seems like it is not possible to execute a local script remotely while allowing an interactive shell and passing arguments.

Interactive (arguments try to run as a seperate command either inside or outside the double quotes)

ssh -t server "$(<${scriptname})"

Arguments (Not interactive ofc)

cat $scriptname | ssh -t server bash -s - "${args[@]}"

I have really tried this every which way. Is it possible to both start an interactive shell and execute the script while also sending arguments?

Best Answer

I believe to have answered my own question by considering changing the values of of the $@ arguments on the remote to the ones locally passed and it seems to work in my tests.

Here is my final solution on just switching out $1 and $2

Local Script

#!/bin/bash

echo "I wanna read you $1 and $2"
read yn

if [ "$yn" == "y" ]; then
    echo "I read you"
else
    echo "I could not read you"
fi

Command

ssh -t $server "set -- "${@:1:2}"; $(<${script})"

Result (when sending test1 and test2 as arguments)

I wanna read you test1 and test2

y

I read you!

Hopefully this helps any future visitors