Stop mangling your variables when passing them to ssh
Shell Scripting / Bash Tricks
Stop mangling your variables when passing them to ssh
🧩 The Challenge
Trying to run a local script on a remote box is a total headache because bash eats your quotes and local variables don’t magically teleport across the wire. I spent half a morning once just trying to pass a simple array into a remote subshell before I finally snapped.
💡 The Fix
Use bash’s ability to feed the local script directly into stdin and let ssh handle the heavy lifting of passing the entire block over. It saves you from having to escape backslashes until your eyes bleed.
ssh user@remote-host 'bash -s' < local_script.sh --arg1 --arg2
⚙️ Why It Works
By sending the file content through the stdin pipe and invoking bash -s on the other end, the remote side interprets the script as if it were a local file, bypassing the usual quoting hell that happens when you pass commands as arguments.
🚀 Pro-Tip: Stick an ‘exit’ at the end of your local script if you need to ensure the remote shell doesn’t hang around waiting for more input.
Linux Tips & Tricks | © ngelinux.com | 8/25/2026
