if-then-else inside Bash Alias
I am trying to make a alias for mv
so it does its normal behavioer in normal folders and is replaced by git mv
inside git repositories. I tried many ways. the if statement works, only the command git mv
will not run correctly.
alias mv='"$(
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
echo mv;
else
echo "git mv";
fi)"'
bash git alias
add a comment |
I am trying to make a alias for mv
so it does its normal behavioer in normal folders and is replaced by git mv
inside git repositories. I tried many ways. the if statement works, only the command git mv
will not run correctly.
alias mv='"$(
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
echo mv;
else
echo "git mv";
fi)"'
bash git alias
7
Use a function, not an alias.
– Satō Katsura
Mar 13 '17 at 18:41
... Or even a little custom script
– roaima
Mar 13 '17 at 18:51
add a comment |
I am trying to make a alias for mv
so it does its normal behavioer in normal folders and is replaced by git mv
inside git repositories. I tried many ways. the if statement works, only the command git mv
will not run correctly.
alias mv='"$(
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
echo mv;
else
echo "git mv";
fi)"'
bash git alias
I am trying to make a alias for mv
so it does its normal behavioer in normal folders and is replaced by git mv
inside git repositories. I tried many ways. the if statement works, only the command git mv
will not run correctly.
alias mv='"$(
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
echo mv;
else
echo "git mv";
fi)"'
bash git alias
bash git alias
edited Mar 13 '17 at 19:05
Vlastimil
7,6911260133
7,6911260133
asked Mar 13 '17 at 18:40
switch87
525316
525316
7
Use a function, not an alias.
– Satō Katsura
Mar 13 '17 at 18:41
... Or even a little custom script
– roaima
Mar 13 '17 at 18:51
add a comment |
7
Use a function, not an alias.
– Satō Katsura
Mar 13 '17 at 18:41
... Or even a little custom script
– roaima
Mar 13 '17 at 18:51
7
7
Use a function, not an alias.
– Satō Katsura
Mar 13 '17 at 18:41
Use a function, not an alias.
– Satō Katsura
Mar 13 '17 at 18:41
... Or even a little custom script
– roaima
Mar 13 '17 at 18:51
... Or even a little custom script
– roaima
Mar 13 '17 at 18:51
add a comment |
2 Answers
2
active
oldest
votes
I would use a function for that, like so:
gitmv()
{
# If in a git repo - call git mv. otherwise- call mv
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
then
mv "$@"
else
git mv "$@"
fi
}
Edit:
alias mv=gitmv
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
1
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
add a comment |
Although jhscheer proposed a much better solution (IMHO), it is still possible to achieve what you were trying to do with only an alias.
Your alias did not work because of the external double quotes, which make git mv
a single token, and you have no git mv
executable. If you remove the external double quotes, your alias should work fine.
By the way, the inner double quotes, in echo "git mv"
are useless, but harmless.
I have no git repository immediately available to test, but I just tried with the following alias:
alias foo='$(if [ x"`pwd`" = x"$HOME" ] ; then echo echo; else echo echo foo; fi)'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f351215%2fif-then-else-inside-bash-alias%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
I would use a function for that, like so:
gitmv()
{
# If in a git repo - call git mv. otherwise- call mv
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
then
mv "$@"
else
git mv "$@"
fi
}
Edit:
alias mv=gitmv
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
1
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
add a comment |
I would use a function for that, like so:
gitmv()
{
# If in a git repo - call git mv. otherwise- call mv
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
then
mv "$@"
else
git mv "$@"
fi
}
Edit:
alias mv=gitmv
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
1
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
add a comment |
I would use a function for that, like so:
gitmv()
{
# If in a git repo - call git mv. otherwise- call mv
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
then
mv "$@"
else
git mv "$@"
fi
}
Edit:
alias mv=gitmv
I would use a function for that, like so:
gitmv()
{
# If in a git repo - call git mv. otherwise- call mv
if [ x`git rev-parse --show-toplevel 2> /dev/null` = x ];
then
mv "$@"
else
git mv "$@"
fi
}
Edit:
alias mv=gitmv
edited 1 hour ago
Tadaboody
32
32
answered Mar 13 '17 at 18:52
jhscheer
963
963
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
1
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
add a comment |
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
1
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
"There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases
– jhscheer
Mar 13 '17 at 18:56
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
indeed, like Sato Katsura also recomanded me I already did it that way. I will edit your answer with my solution (almost the same but with alias)
– switch87
Mar 13 '17 at 19:02
1
1
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
@switch87: I'm glad you found a suitable solution for you. It seems your edit got rejected due to a conflict with another (random) edit. I'll add your alias.
– jhscheer
Mar 13 '17 at 19:28
add a comment |
Although jhscheer proposed a much better solution (IMHO), it is still possible to achieve what you were trying to do with only an alias.
Your alias did not work because of the external double quotes, which make git mv
a single token, and you have no git mv
executable. If you remove the external double quotes, your alias should work fine.
By the way, the inner double quotes, in echo "git mv"
are useless, but harmless.
I have no git repository immediately available to test, but I just tried with the following alias:
alias foo='$(if [ x"`pwd`" = x"$HOME" ] ; then echo echo; else echo echo foo; fi)'
add a comment |
Although jhscheer proposed a much better solution (IMHO), it is still possible to achieve what you were trying to do with only an alias.
Your alias did not work because of the external double quotes, which make git mv
a single token, and you have no git mv
executable. If you remove the external double quotes, your alias should work fine.
By the way, the inner double quotes, in echo "git mv"
are useless, but harmless.
I have no git repository immediately available to test, but I just tried with the following alias:
alias foo='$(if [ x"`pwd`" = x"$HOME" ] ; then echo echo; else echo echo foo; fi)'
add a comment |
Although jhscheer proposed a much better solution (IMHO), it is still possible to achieve what you were trying to do with only an alias.
Your alias did not work because of the external double quotes, which make git mv
a single token, and you have no git mv
executable. If you remove the external double quotes, your alias should work fine.
By the way, the inner double quotes, in echo "git mv"
are useless, but harmless.
I have no git repository immediately available to test, but I just tried with the following alias:
alias foo='$(if [ x"`pwd`" = x"$HOME" ] ; then echo echo; else echo echo foo; fi)'
Although jhscheer proposed a much better solution (IMHO), it is still possible to achieve what you were trying to do with only an alias.
Your alias did not work because of the external double quotes, which make git mv
a single token, and you have no git mv
executable. If you remove the external double quotes, your alias should work fine.
By the way, the inner double quotes, in echo "git mv"
are useless, but harmless.
I have no git repository immediately available to test, but I just tried with the following alias:
alias foo='$(if [ x"`pwd`" = x"$HOME" ] ; then echo echo; else echo echo foo; fi)'
answered 1 hour ago
user2233709
633310
633310
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f351215%2fif-then-else-inside-bash-alias%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
7
Use a function, not an alias.
– Satō Katsura
Mar 13 '17 at 18:41
... Or even a little custom script
– roaima
Mar 13 '17 at 18:51