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.

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.