How can I alias a command that uses custom input in terminal?
For example, let's say a command
$ oj d https://codeforces.com/contest/1348/problem/A
In this command 1348 and 'A' can change. How can I achieve this?
Something like I can alias the whole command as,
$ oj d 1348 A
such that it can act like same.
-
Duplicate: Make a Bash alias that takes a parameter?Devon– Devon2020年05月04日 09:43:02 +00:00Commented May 4, 2020 at 9:43
1 Answer 1
Aliases do not accept parameters. You need to write a function or a separate script.
Bash/Zsh function:
Define the following function in your .bashrc
/.zshrc
depending on what interactive shell you use:
mycommand() {
oj d https://codeforces.com/contest/"1ドル"/problem/"2ドル"
}
Shell script:
Create the file mycommand
with the following content in your $PATH
and make it executable:
#!/bin/sh
oj d https://codeforces.com/contest/"1ドル"/problem/"2ドル"
1ドル
and 2ドル
are positional parameters. If you call the script or function like $ mycommand 1348 A
, 0ドル
gets mapped to mycommand, 1ドル
to 1348, 2ドル
to A and so on. We put the double quotes around the variables in case they contain whitespace to prevent word splitting.