Previously defined variable in bash script not expanded when I try to execute a command through SSH

74 Views Asked by At
#!/bin/bash
CLIP=$(termux-clipboard-get)
ssh user@ip 'cat>~/Scripts/clip.log<<<$CLIP'

This is a very basic bash script to forward my system clipboard from my phone (through termux) into a file on my desktop through SSH.

The issue is the third line, I can't get it to write what's stored in the variable and it ends up writing nothing.

I've tested:

  • echo $CLIP, and the clipboard request works.
  • replacing $CLIP with a string, and clip.log updates properly.
1

There are 1 best solutions below

0
Stephen Ostermiller On

It would work better if you used a pipe rather than a variable. Then you don't need to worry as much about escaping or quotes.

termux-clipboard-get | ssh user@ip 'cat > ~/Scripts/clip.log'

If you want it to append to the log file, you could use >> rather than >.