Stop chasing your tail when variables expand too early
Shell Scripting / Bash Tricks
Stop chasing your tail when variables expand too early
🧩 The Challenge
Trying to pass a complex command string into a remote SSH session is a nightmare because your local shell tries to expand the variables before they even get to the server. I’ve wasted an entire afternoon once trying to debug why my remote logs were empty, only to realize the backticks were firing locally.
💡 The Fix
Use single quotes to keep your variables literal so they expand exactly when they reach their destination. It saves you from the headache of escaping backslashes manually.
ssh user@remote "bash -c 'echo \"Current path is \$PWD\"'"
⚙️ Why It Works
Single quotes act as a shield, preventing your local bash from touching the inner content, which forces the remote shell to handle the variable interpolation instead. By escaping the dollar sign inside the double quotes, you tell the local shell to leave it alone until the remote process runs it.
🚀 Pro-Tip: Stick to double-quoting your heredocs if you actually want variable substitution to happen.
Linux Tips & Tricks | © ngelinux.com | 9/13/2026
