0

I know questions regarding this error have been asked before - but I can't seem to figure out why this isn't working in this case: for aliases.

I have the following

alias scpip='scp $1 [email protected]:~'

I want the use case to be scpip file so that it copies the file into user's home directory in ip.edu. Instead I get the error

scp: /home/user: not a regular file

It works if I do it 'manually' ie scp file [email protected]:~ . How can I make this work as an alias with an argument?

2
  • 3
    shell aliases don't have arguments; the $1 in your alias expands to the first arg (if it exists) of the script in which you invoke it, or if used interactively (as aliases usually are) to nothing. It is followed by any arguments given in the command. Thus you are actually running scp [email protected]:~ file which tries to copy the remote directory to a local directory confusingly named file, but fails. You may want a function or script instead. Commented Apr 6, 2022 at 2:09
  • 2
    An alias replaces text with text, there is no logic. Use a function instead. And quote right when defining the function. And then quote even more right at runtime because scp is so cumbersome it requires you to quote for the remote shell (see this answer from where it states "better don't use scp at all"). Commented Apr 6, 2022 at 14:02

1 Answer 1

1

If when running the command, the first argument or $1 is a directory, which it looks like it is, then you'll need to use the -r switch in order to copy it whether it contains data or not. It can be run as an alias but it's more complicated and not worth the effort. It's better to just create a script.

Create a file called scpip in /home/directory

Have the contents as:

scp -r $1 [email protected]:~

Add the execute bit:

chmod u+x /home/directory/scpip

In your ~/.bashrc, add the line

export PATH=/home/directory:$PATH

Source the init:

. ~/.bashrc

Lastly, run the script with:

scpip file

That will use the first argument. If it's a file,

scp -r file [email protected]:~

If a directory

scp -r directory [email protected]:~
1
  • ~ is always a directory and your solution will copy it from ip.edu to the local system, which is not what OP says they want Commented Apr 6, 2022 at 2:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .