hbaromega
hbaromega

Reputation: 2374

Shell script: shorten or aliasing an address after a command

I want to abbreviate or set an alias to a destination address every time I use while copying files. For example,

scp <myfile> my_destination 

where my_destination could be [email protected]:Documents. So I want to modify my .bash_profile by inserting something like

alias my_destination = '[email protected]:Documents' .

But that doesn't work since my_destination is not a command.

Is there a way out?

Note: I don't want to abbreviate the whole command, but only the address, so that I can use it with other possible commands.

Upvotes: 0

Views: 385

Answers (4)

hbaromega
hbaromega

Reputation: 2374

I think this works without using export as well since anyway I am assigning a variable for the path or destination. So I can just put the following in my .basrc or .bash_profile :

my_destination='[email protected]:Documents/'

Then

scp <myfile> $my_destination 

Similarly I can execute any action (e.g. moving a file)for any local destination or directory:

local_dest='/Users/hbaromega/Documents/' 

and then

mv <myfile>  $local_dest 

In summary, a destination address can be put as a variable, but not as a shell command or function.

Upvotes: 0

whereswalden
whereswalden

Reputation: 4959

You have a couple options. You can set hostname aliases in your ~/.ssh/config like this:

Host my_destination
  Hostname 192.168.1.100
  User hbaromega

You could use it like this:

$ scp myfile my_destination:Documents/

Note that you'd still have to specify the default directory.

Another option would be to just put an environment variable in your ~/.bashrc:

export my_destination='[email protected]:Documents/'

Then you could use it like this:

$ scp myfile $my_destination

BertD's approach of defining a function would also work.

Upvotes: 0

David C. Rankin
David C. Rankin

Reputation: 84551

The reason it does not work is there are spaces surrounding the = sign. As pointed out, an alias must be called as the first part of the command string. You are more likely to get the results you need by exporting my_destination and then calling it with a $. In ~/.bashrc:

export my_destination='[email protected]:Documents'

Then:

scp <myfile> $my_destination

Note: you will likely need to provide a full path to Documents in the export.

Upvotes: -1

BertD
BertD

Reputation: 628

You can't do what you want for the reason you state (an alias defines an entire command). But you could use a shell function to come close:

my_scp() {
  scp "$@" [email protected]:Documents/.
}

which you could then call as

my_scp *.c

(Using $@ in doublequotes is shell black magic that avoids trouble if any of the file names matched by the *.c glob contain spaces)

Of course, if you don't want to define a function, you could always use a shell variable to at least save the retyping:

dest='[email protected]:Documents/.'
scp *.c $dest

Upvotes: 2

Related Questions