How to set part of a path dynamically for alias command in bashrc file
I'm trying to have a alias command and the part of its path keeps changing.
Eg:
/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/
and my alias command to achieve is something link this
alias cws='cd /var/mywork/swag//
since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?
I tried something like
alias cws='cd /var/mywork/swag/getenv("WSP")/
so whenever I set WSP to required path it automatically takes. But it didn't help.
bash shell
add a comment |
I'm trying to have a alias command and the part of its path keeps changing.
Eg:
/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/
and my alias command to achieve is something link this
alias cws='cd /var/mywork/swag//
since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?
I tried something like
alias cws='cd /var/mywork/swag/getenv("WSP")/
so whenever I set WSP to required path it automatically takes. But it didn't help.
bash shell
add a comment |
I'm trying to have a alias command and the part of its path keeps changing.
Eg:
/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/
and my alias command to achieve is something link this
alias cws='cd /var/mywork/swag//
since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?
I tried something like
alias cws='cd /var/mywork/swag/getenv("WSP")/
so whenever I set WSP to required path it automatically takes. But it didn't help.
bash shell
I'm trying to have a alias command and the part of its path keeps changing.
Eg:
/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/
and my alias command to achieve is something link this
alias cws='cd /var/mywork/swag//
since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?
I tried something like
alias cws='cd /var/mywork/swag/getenv("WSP")/
so whenever I set WSP to required path it automatically takes. But it didn't help.
bash shell
bash shell
edited Nov 15 '18 at 7:31
melpomene
59.5k54691
59.5k54691
asked Nov 15 '18 at 7:07
KhrusosKhrusos
275
275
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Like most problems with aliases, this is easier to solve with a shell function:
cws() {
cd /var/mywork/swag/"$WSP"
}
add a comment |
Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do
alias cws='cd /var/mywork/swag/$WSP'
But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write
WSP=aaa
...
WSP=xxx
...
WSP=yyy cws
and cws
is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:alias cws="cd /var/mywork/swag/$WSP"
Quoting the$
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)
– Paul Hodges
Nov 15 '18 at 14:33
1
Sure, a function with an argument such as${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to changeWSP
often, so his function would usually be invoked without arguments. Another option to consider would be to setCDPATH=.:/var/mywork/swag
and get rid of alias and function.
– user1934428
Nov 16 '18 at 7:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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%2fstackoverflow.com%2fquestions%2f53314097%2fhow-to-set-part-of-a-path-dynamically-for-alias-command-in-bashrc-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Like most problems with aliases, this is easier to solve with a shell function:
cws() {
cd /var/mywork/swag/"$WSP"
}
add a comment |
Like most problems with aliases, this is easier to solve with a shell function:
cws() {
cd /var/mywork/swag/"$WSP"
}
add a comment |
Like most problems with aliases, this is easier to solve with a shell function:
cws() {
cd /var/mywork/swag/"$WSP"
}
Like most problems with aliases, this is easier to solve with a shell function:
cws() {
cd /var/mywork/swag/"$WSP"
}
answered Nov 15 '18 at 7:30
melpomenemelpomene
59.5k54691
59.5k54691
add a comment |
add a comment |
Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do
alias cws='cd /var/mywork/swag/$WSP'
But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write
WSP=aaa
...
WSP=xxx
...
WSP=yyy cws
and cws
is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:alias cws="cd /var/mywork/swag/$WSP"
Quoting the$
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)
– Paul Hodges
Nov 15 '18 at 14:33
1
Sure, a function with an argument such as${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to changeWSP
often, so his function would usually be invoked without arguments. Another option to consider would be to setCDPATH=.:/var/mywork/swag
and get rid of alias and function.
– user1934428
Nov 16 '18 at 7:52
add a comment |
Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do
alias cws='cd /var/mywork/swag/$WSP'
But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write
WSP=aaa
...
WSP=xxx
...
WSP=yyy cws
and cws
is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:alias cws="cd /var/mywork/swag/$WSP"
Quoting the$
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)
– Paul Hodges
Nov 15 '18 at 14:33
1
Sure, a function with an argument such as${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to changeWSP
often, so his function would usually be invoked without arguments. Another option to consider would be to setCDPATH=.:/var/mywork/swag
and get rid of alias and function.
– user1934428
Nov 16 '18 at 7:52
add a comment |
Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do
alias cws='cd /var/mywork/swag/$WSP'
But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write
WSP=aaa
...
WSP=xxx
...
WSP=yyy cws
and cws
is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.
Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do
alias cws='cd /var/mywork/swag/$WSP'
But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write
WSP=aaa
...
WSP=xxx
...
WSP=yyy cws
and cws
is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.
answered Nov 15 '18 at 7:45
user1934428user1934428
4,00721632
4,00721632
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:alias cws="cd /var/mywork/swag/$WSP"
Quoting the$
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)
– Paul Hodges
Nov 15 '18 at 14:33
1
Sure, a function with an argument such as${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to changeWSP
often, so his function would usually be invoked without arguments. Another option to consider would be to setCDPATH=.:/var/mywork/swag
and get rid of alias and function.
– user1934428
Nov 16 '18 at 7:52
add a comment |
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:alias cws="cd /var/mywork/swag/$WSP"
Quoting the$
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)
– Paul Hodges
Nov 15 '18 at 14:33
1
Sure, a function with an argument such as${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to changeWSP
often, so his function would usually be invoked without arguments. Another option to consider would be to setCDPATH=.:/var/mywork/swag
and get rid of alias and function.
– user1934428
Nov 16 '18 at 7:52
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:
alias cws="cd /var/mywork/swag/$WSP"
Quoting the $
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)– Paul Hodges
Nov 15 '18 at 14:33
Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on:
alias cws="cd /var/mywork/swag/$WSP"
Quoting the $
makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)– Paul Hodges
Nov 15 '18 at 14:33
1
1
Sure, a function with an argument such as
${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP
often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag
and get rid of alias and function.– user1934428
Nov 16 '18 at 7:52
Sure, a function with an argument such as
${1:-${WSP:-some_nice_default}}
would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP
often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag
and get rid of alias and function.– user1934428
Nov 16 '18 at 7:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53314097%2fhow-to-set-part-of-a-path-dynamically-for-alias-command-in-bashrc-file%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