1

In order to speed up typing of a long command, we can make an alias of a command, e.g.

alias remcopy='scp [email protected]:/home/file.txt /home/user/'

And we type user this exact command will be run.

However, is it possible only to load the command and then modify it according to the current need, e.g.

scp [email protected]:/home/file.txt /home/user/addeddir/
cerebrou
  • 459
  • 1
  • 7
  • 16
  • 1
    It sounds like what you are really asking is [Can I pass arguments to an alias command?](https://askubuntu.com/questions/626458/can-i-pass-arguments-to-an-alias-command) – steeldriver Dec 04 '19 at 15:42

2 Answers2

1

There are two ways you can start various modifications of the command.

  • an alias can have a parameter, for example

    alias remcopy='scp [email protected]:/home/user/file.txt'
    
    remcopy targetname
    

    where targetname is selected a run time.

  • a function is more flexible than an alias. It can be a single line or big like a whole shellscript file.

    function remcopy () { scp [email protected]:/home/user/file.txt /home/user/"$1" ; }
    
    remcopy
    remcopy addeddir
    

    which can be used without a parameter and with a parameter (in order to change the name of the target file.

    You can store a small function in ~/.bashrc like you store aliases.

sudodus
  • 45,126
  • 5
  • 87
  • 151
  • 1
    This might also be handy: `remcopy() { ssh host:remotefile "${1:-/home/user}"; }` to provide a default value if there's no specified value. – glenn jackman Dec 04 '19 at 17:45
0

You can use variables to accomplish this.

For example:

alias something='nano ${1}'
something test.txt

Will open test.txt in nano for writing

stratus
  • 549
  • 2
  • 5