How to create alias that takes a positional argument in Linux bash?
Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):
my_cmd option1 option2 filename
I created an alias:
alias my_cmd_12="my_cmd option1 option2"
this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):
my_cmd option1 option2 filename --flag1
How do I create an alias that takes all option an flags:
my_alias filename
is equivalent to
my_cmd option1 option2 filename --flag1
linux command-line bash
add a comment |
Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):
my_cmd option1 option2 filename
I created an alias:
alias my_cmd_12="my_cmd option1 option2"
this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):
my_cmd option1 option2 filename --flag1
How do I create an alias that takes all option an flags:
my_alias filename
is equivalent to
my_cmd option1 option2 filename --flag1
linux command-line bash
add a comment |
Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):
my_cmd option1 option2 filename
I created an alias:
alias my_cmd_12="my_cmd option1 option2"
this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):
my_cmd option1 option2 filename --flag1
How do I create an alias that takes all option an flags:
my_alias filename
is equivalent to
my_cmd option1 option2 filename --flag1
linux command-line bash
Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):
my_cmd option1 option2 filename
I created an alias:
alias my_cmd_12="my_cmd option1 option2"
this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):
my_cmd option1 option2 filename --flag1
How do I create an alias that takes all option an flags:
my_alias filename
is equivalent to
my_cmd option1 option2 filename --flag1
linux command-line bash
linux command-line bash
asked 2 hours ago
motam79
1649
1649
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can't do this with alias. Alias works by replacing string with another string. With this alias defined
alias my_cmd_12="my_cmd option1 option2"
my_cmd_12 filename --flag1
will expand to
my_cmd option1 option2 filename --flag1
But you want to invoke my_alias filename
to get the same result. There is no way to replace my_alias
with another string so --flag1
appears at the end.
However a function should work:
my_function() { my_cmd option1 option2 "$1" --flag1; }
Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@"
, conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.
More information here: In Bash, when to alias, when to script, and when to write a function?
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1387609%2fhow-to-create-alias-that-takes-a-positional-argument-in-linux-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't do this with alias. Alias works by replacing string with another string. With this alias defined
alias my_cmd_12="my_cmd option1 option2"
my_cmd_12 filename --flag1
will expand to
my_cmd option1 option2 filename --flag1
But you want to invoke my_alias filename
to get the same result. There is no way to replace my_alias
with another string so --flag1
appears at the end.
However a function should work:
my_function() { my_cmd option1 option2 "$1" --flag1; }
Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@"
, conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.
More information here: In Bash, when to alias, when to script, and when to write a function?
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
add a comment |
You can't do this with alias. Alias works by replacing string with another string. With this alias defined
alias my_cmd_12="my_cmd option1 option2"
my_cmd_12 filename --flag1
will expand to
my_cmd option1 option2 filename --flag1
But you want to invoke my_alias filename
to get the same result. There is no way to replace my_alias
with another string so --flag1
appears at the end.
However a function should work:
my_function() { my_cmd option1 option2 "$1" --flag1; }
Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@"
, conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.
More information here: In Bash, when to alias, when to script, and when to write a function?
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
add a comment |
You can't do this with alias. Alias works by replacing string with another string. With this alias defined
alias my_cmd_12="my_cmd option1 option2"
my_cmd_12 filename --flag1
will expand to
my_cmd option1 option2 filename --flag1
But you want to invoke my_alias filename
to get the same result. There is no way to replace my_alias
with another string so --flag1
appears at the end.
However a function should work:
my_function() { my_cmd option1 option2 "$1" --flag1; }
Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@"
, conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.
More information here: In Bash, when to alias, when to script, and when to write a function?
You can't do this with alias. Alias works by replacing string with another string. With this alias defined
alias my_cmd_12="my_cmd option1 option2"
my_cmd_12 filename --flag1
will expand to
my_cmd option1 option2 filename --flag1
But you want to invoke my_alias filename
to get the same result. There is no way to replace my_alias
with another string so --flag1
appears at the end.
However a function should work:
my_function() { my_cmd option1 option2 "$1" --flag1; }
Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@"
, conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.
More information here: In Bash, when to alias, when to script, and when to write a function?
answered 1 hour ago
Kamil Maciorowski
23.6k155074
23.6k155074
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
add a comment |
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
– Hannu
57 mins ago
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1387609%2fhow-to-create-alias-that-takes-a-positional-argument-in-linux-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown