Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding things to basic bash commands without creating infinite loops

Tags:

bash

ls

cd

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?

like image 807
patapouf_ai Avatar asked Sep 18 '26 08:09

patapouf_ai


1 Answers

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]
like image 60
Aaron Avatar answered Sep 21 '26 00:09

Aaron