Stop infinite recursion in Bash Script
So I fell into this classic trap. While trying to teach myself basic bash scripting, I figured I wanted to do this:
alias emacs=emacsFunction
emacsFunction() {
if [ "$#" -eq 1 ]; then
emacs $1 &
fi
emacs
}
The goal is pretty simple. When I type emacs into the terminal, if I supply an argument (like emacs.bashrc), I want it to open my .bashrc file in emacs and still allow me to type commands in my terminal window (adding '&' to the command allows this is).
I am open to all suggestions. I'm sure there is a way more efficient (smarter) way of doing this than the code I have here.
Thanks in advance!
source to share
Avoiding functions / aliases / etc. that's what it's built for command
(see Bash Built-in Commands ). So use command emacs
if you want to avoid your function or alias.
That being said, you don't need both the function and the alias here. Your function might just be called emacs.
You also don't need to explicitly check for arguments, unless you want it to go to the background when passing arguments (and not otherwise). (I am assuming it spawns window X in the background?)
If you want the background-only-for-arguments version, then you want:
emacs() {
if [ $# -eq 0 ]; then
command emacs
else
command emacs "$@" &
fi
}
Assuming you don't need a background-only business, you can simply use:
emacs() {
command emacs "$@" &
}
If you don't want the background behavior for arguments only, this is not entirely different, it just always works emacs &
or emacs <files> &
since you only store the input &
to the end.
source to share