I would like to make a script to modify some basic command-line commands like ls or cd which would end up calling the normal ls or cd commands but without creating infinite loops. How could I do that?
So, what I was thinking of doing is something like creating a cd2.sh file like:
#!/bin/bash
echo 'Oh! Your are changing directories'
cd "$@"
and then put an alias in .bashrc (or .bash_profile for mac):
alias cd = 'cd2.sh'
but this creates an infinite loop and my computer is not very happy about that. Is there an easy way to achieve my goal?
You can use the command command, or its shortcut notation \. Since cd is a builtin, you could also use the builtin command. They ignore functions and aliases and instead look up an executable directly in the $PATH and/or the built-ins.
$ alias ls='echo no ls for you!' $ ls no ls for you! $ command ls [directory listing] $ \ls [directory listing]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With