Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash Function/Alias name collision causes seg fault

The problem in this post also happened to me.
mkdir crashes bash

I couldn’t comment there because no reputation.
Can anyone elucidate why this happens? It happens at runtime Not when the function is loaded.
Here is the paste of the identical issue.

md() 
{  
    [ $# -eq 0 ] && { echo "$0 dirname [permissions]"; return; }
    [ -d "$1" ] && { echo "%1 already exists!"; return; }
    mkdir -m ${2:-0755} -p "$1"
}

"Problem was that I had previously had an alias like alias md="mkdir" so when bash parsed the new bash function, it expanded the md() to mkdir() and the function became infinitely recursive and crashed the shell. "

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I felt this issue was aptly found but i do not understand the reasoning.
I don’t understand any possible reason the alias expansion is misdirecting the fully qualified shell function. Backwards right?

Sorry for the dupe, please read why before a callout.

>Solution :

"Aliases are really just text replacement macros – so when you try to define md() { ... } after defining md=mkdir you actually end up with mkdir() { ... }. Try printing out the function defs with declare -p -f md and declare -p -f mkdir to see what I mean." – steeldriver

zi@zi-top:~$ declare -p -f nd
bash: declare: nd: not found
zi@zi-top:~$ declare -p -f mkdir
mkdir () 
{ 
    mkdir -p "$@"
}
zi@zi-top:~$ nd () {
        mkdir -p "$@"
}
zi@zi-top:~$ declare -p -f nd
bash: declare: nd: not found

I see that what steeldriver said is true, i think it`s a strange architectural decision with prob some kind of reasoning in the code is text bashism wurl.
Thanks!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading