1

In Linux terminal, normally I can copy files from another unix server using:

scp -r [email protected]:/data/filename ./

which secure copies the remote file to my current directory. I've made sure this command line works well before I did anything below.

To save time of typing the long front part, I create an alias for scp -r [email protected]:, and receive arguments for /data/filename and ./ from command line.

Opening the ~/.bashrc file, first I tried

alias scpfile="scp -r [email protected]:'$1' '$2'"

In command line, I typed scpfile /data/filename ./, but I got an error message as follows:

error: unexpected filename: .

Then I tried another one

function scpfile() {
    scp -r [email protected]:$1 $(pwd)
}

or

function scpfile() {
    scp -r [email protected]:"$1" "$(pwd)"
}

In command line, I typed scpfile /data/filename, but it's also wrong with error message:

/data/filename: No such file or directory

Can someone help if you know the answer to it?

Moreover, I've tried the answers shown in Make a Bash alias that takes a parameter, but still not working!

10
  • I noticed this post and tried the answers. However, none of them worked in my case.
    – Leo
    Commented Jul 6 at 20:40
  • If I'm correct, your second function "doesn't work" due to missing quotes (scp -r [email protected]:"$1" "$(pwd)" should be fine). If that also fails for you, post more details, including full error message.
    – STerliakov
    Commented Jul 6 at 20:43
  • 1
    The error is more indicative of the remote file not existing than any problem with your function definition (though $1 and $(pwd)--or better, $PWD--should both be quoted).
    – chepner
    Commented Jul 6 at 20:54
  • 2
    It may be easier to configure the user name for that server in your ~/.ssh/config and then the command is just scp edu:/some/file .
    – Robert
    Commented Jul 6 at 20:55
  • 2
    @yvs2014 It doesn't make sense for an alias to be redefining a function each time that alias it iinvoked. Why wouldn't you just call that function home2edu and drop the alias.
    – Kaz
    Commented Jul 6 at 22:21

1 Answer 1

0

The Bash documentation (Info manual) says:

There is no mechanism for using arguments in the replacement text, as in csh. If arguments are needed, use a shell function (see Shell Functions).

and

For almost every purpose, shell functions are preferred over aliases.

If you need to develop a custom command that takes argument to which it can refer by position, you write a function.

This looks right:

function scpfile() {
    scp -r [email protected]:"$1" "$(pwd)" 
}

Except, there is no advantage of using "$(pwd)" over just . (period), and it is less efficient, too. You would only need that in order to given a path argument to some tool that insists on requiring absolute paths.

The error message about the non-existence of $1 is coming from the remote host. scp is finding that /data/filename does not exist on the remote host.

2
  • The function keyword and () are redundant. Use either one, though the later is the preferred most portable way. See: askubuntu.com/questions/1023942/…
    – Léa Gris
    Commented Jul 7 at 8:31
  • @LéaGris Good catch.
    – Kaz
    Commented Jul 7 at 8:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.