0

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.

blue edge
  • 59
  • 7
  • Duplicate: [Make a Bash alias that takes a parameter?](https://stackoverflow.com/questions/7131670) – Devon May 04 '20 at 09:43

1 Answers1

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.

Devon
  • 130
  • 9