I have a wrapper script called my_mv.sh like below(I am using bash):
#/bin/bash
function my_mv(){
FILE="${@: -1}" # bash or ksh,zsh
echo $FILE
if [ -f $FILE ];
then
mv -i $@
else
mv $@
fi
}
When I use it as a script and run it directly like ./my_mv.sh file1 file2, the result is as expected. However when I put function my_mv into ~/.bashrc and source it, there would be a infinite loop.
So what’s the difference between the two methods? How can I change the script so it can be sourced right?
BTW, when using zsh, there is a similar result for the two approaches.
Did you actually name it
my_mvin your .bashrc, or did you in fact name itmv, to override the default?If so, use
command mvinstead of justmvin your function to invoke the system version instead of recursing.If you just run this as
./my_mv.sh file1 file2, it will not do anything, because the function is defined but not called.