1

I ran the following Bash function that adds a string with expanded variables, into the end of my bashrc:

alias() {
    echo "alias $repo=\"$HOME\"/$repo/$repo.sh" >> "$HOME"/.bashrc
    source "$HOME"/.bashrc 2>/dev/null
}
alias

To run it I copied it, pasted in the Bash terminal (there it appeared once) and executed by hitting Enter.

The output I got in ~/.bashrc was about a thousand lines of the above string:

alias $repo=\"$HOME\"/$repo/$repo.sh

The very last command (source /home/user/.bashrc) kept being executed endlessly (I assume due to endless calling to the function) so I immediately aborted with the ^C key combo.


After removing all thousand repeats of the string with a Nano mark-set and cut operation, I desire to ask why did this happen (and keep happening)?

  • 2
    alias is a bash builtin; don't name your functions like that. – jasonwryan Mar 4 '18 at 4:40
3

You defined a function called alias, added a line to .bashrc that calls alias ..., and then sourced .bashrc into your shell (which has the function defined in it already). The alias you sourced calls the function, which adds another line and sources the script again, calling the function again once for each time it's already run, leading to exponential growth.

Change the name of your function.

  • I changed the name in the routine and call to setAlias but still had the problem. Only after I ran reboot I no longer had that problem. I don't know why a reboot was needed. – user9303970 Mar 4 '18 at 7:21
  • 1
    Because the function was still there in your shell. – Michael Homer Mar 4 '18 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.