if-then-else inside Bash Alias












5














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)"'









share|improve this question




















  • 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
















5














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)"'









share|improve this question




















  • 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














5












5








5







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)"'









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 13 '17 at 19:05









Vlastimil

7,8271261134




7,8271261134










asked Mar 13 '17 at 18:40









switch87switch87

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














  • 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










2 Answers
2






active

oldest

votes


















6














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






share|improve this answer























  • "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



















1














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)'





share|improve this answer





















  • Thank you for explaining why my first try did not work.
    – switch87
    Jan 1 at 14:06











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
});


}
});














draft saved

draft discarded


















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









6














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






share|improve this answer























  • "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
















6














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






share|improve this answer























  • "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














6












6








6






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 19:09









Tadaboody

33




33










answered Mar 13 '17 at 18:52









jhscheerjhscheer

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


















  • "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













1














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)'





share|improve this answer





















  • Thank you for explaining why my first try did not work.
    – switch87
    Jan 1 at 14:06
















1














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)'





share|improve this answer





















  • Thank you for explaining why my first try did not work.
    – switch87
    Jan 1 at 14:06














1












1








1






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)'





share|improve this answer












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)'






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 19:33









user2233709user2233709

803311




803311












  • Thank you for explaining why my first try did not work.
    – switch87
    Jan 1 at 14:06


















  • Thank you for explaining why my first try did not work.
    – switch87
    Jan 1 at 14:06
















Thank you for explaining why my first try did not work.
– switch87
Jan 1 at 14:06




Thank you for explaining why my first try did not work.
– switch87
Jan 1 at 14:06


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre