Trying to create a scp command in bash

684 views Asked by At

I am trying to create a simple bash script that takes paths to directories as input and plugs them into an scp command. The scp command looks like this:

scp [email protected]:/path/to/directory/on/server /path/to/directory/on/local/machine

I have made a simple bash function,

s2h () {
    local address_in_server="$1"
    local address_in_home="$2"
    echo "$address_in_server"
    echo "$address_in_home"
    scp [email protected]:$address_in_server $address_in_home
}

After running this script as s2h ~/testing/somefile.txt ~/Desktop The echo commands output

/Users/me/testing
/Users/me/Desktop/

This particular pathway, with /Users/me is meant for my local machine. The problem is that bash is interpreting the "~" for the server as the "~" for my local machine. If I enter

s2h /home/myusername/testing/somefile.txt ~/Desktop

It works perfectly.

However, the command

scp [email protected]:~/testing/somefile.txt ~/Desktop 

also works.

My question is, how do I get the bash script to understand what ~ means when calling a file from my server from an argument, and not translate it to the local meaning of ~?

1

There are 1 answers

2
Cyrus On BEST ANSWER

Use quotes to suppress local tilde expansion:

s2h "~/testing/somefile.txt" ~/Desktop