Search code examples
bashsshmv

Moving remote folder named by local variable


I'm trying to move a remote directory whose name contains a value which is a single digit number stored in a local variable.

ssh root@1.1.1.1 'mv /etc/folder_* /etc/folder_$value'

However $value isn't parsed, i've tried putting quotes and double quotes?


Solution

  • The solution is to use double quotes instead of single quotes. So that the shell command can interpolate your locally defined variable "value". So use instead:

    ssh root@1.1.1.1 "mv /etc/folder_* /etc/folder_$value"
    

    Example output for when I tested this: (variable NROFLINES is defined locally, equal to 4)

    cristi@ubuntu:~$ ssh archtln@192.168.174.165 'ls | head -$NROFLINES'
    archtln@192.168.174.165's password: 
    Desktop
    Documents
    Downloads
    examples.desktop
    Music
    Pictures
    Public
    Templates
    tmp
    
    cristi@ubuntu:~$ ssh archtln@192.168.174.165 "ls | head -$NROFLINES"
    archtln@192.168.174.165's password: 
    Desktop
    Documents
    Downloads
    examples.desktop