Today I encountered a strange scenario (rather common for most of programmers) while adding function in my .bashrc file. For confidentiality purpose, I have slightly modified the use-case to present the scenario. Here is what I did
First, I added alias. But it did not suite my needs wherein I wanted to append folder name to the complete path.
alias my_func="echo /home/nandakumar/funny/"
What I achieved with this alias was
nandakumar@heramba:~$ source ~/.bashrc
nandakumar@heramba:~$ my_func nanda
/home/nandakumar/funny/ nanda
My expectation was to print /home/nandakumar/funny/nanda i.e. without added space. However, aliases do not have such capability.
After scouting the digital knowledge book (aka internet), I realized my use case needed function
So I transformed alias to bash function as below
And the output:
nandakumar@heramba:~$ source ~/.bashrc
nandakumar@heramba:~$ my_func nanda
/home/nandakumar/funny/ nanda
Strange!! Output Unchanged!!! I almost pondered 20 mins wondering what went wrong. I checked syntax, the commands umpteen times. Alas! No luck :-(. Eventually I closed and re-opened shell and to my surprise, here was output
nandakumar@heramba:~$ my_func nanda
/home/nandakumar/funny/nanda
The output is clean now 8-).
Later when I re-did the experiment, alias entry was still present in bash despite wiping out from .bashrc file
nandakumar@heramba:~$ alias my_func
alias my_func='echo /home/nandakumar/funny/'
I stumbled upon one of stack-overflow query later in day and figured out that this is expected. A quote states as below from: http://stackoverflow.com/questions/7131670/make-bash-alias-that-takes-parameter
"If you are changing an alias to a function,
Ha!! HA LOL :D. All I needed was explicit unalias to achieve what I wished for. One more important lesson learned. Hope you also gained some insight.
This may look familiar to many, but for me it's new learning.
First, I added alias. But it did not suite my needs wherein I wanted to append folder name to the complete path.
alias my_func="echo /home/nandakumar/funny/"
What I achieved with this alias was
nandakumar@heramba:~$ source ~/.bashrc
nandakumar@heramba:~$ my_func nanda
/home/nandakumar/funny/ nanda
My expectation was to print /home/nandakumar/funny/nanda i.e. without added space. However, aliases do not have such capability.
After scouting the digital knowledge book (aka internet), I realized my use case needed function
So I transformed alias to bash function as below
And the output:
nandakumar@heramba:~$ source ~/.bashrc
nandakumar@heramba:~$ my_func nanda
/home/nandakumar/funny/ nanda
Strange!! Output Unchanged!!! I almost pondered 20 mins wondering what went wrong. I checked syntax, the commands umpteen times. Alas! No luck :-(. Eventually I closed and re-opened shell and to my surprise, here was output
nandakumar@heramba:~$ my_func nanda
/home/nandakumar/funny/nanda
The output is clean now 8-).
Later when I re-did the experiment, alias entry was still present in bash despite wiping out from .bashrc file
nandakumar@heramba:~$ alias my_func
alias my_func='echo /home/nandakumar/funny/'
I stumbled upon one of stack-overflow query later in day and figured out that this is expected. A quote states as below from: http://stackoverflow.com/questions/7131670/make-bash-alias-that-takes-parameter
"If you are changing an alias to a function,
source
ing
your .bashrc will add the function but it won't unalias the old alias.
Since aliases are higher precedent than functions, it will try to use
the alias. You need to either close and reopen your shell, or else call
unalias <name>
. Perhaps I'll save someone the 5 minutes I just wasted."Ha!! HA LOL :D. All I needed was explicit unalias to achieve what I wished for. One more important lesson learned. Hope you also gained some insight.
This may look familiar to many, but for me it's new learning.