I want to be able to see the code that gets executed whenever I type 'ls' or 'cd' in my terminal. With that I would like to get into modifying existing programs and writing new ones. In what language are those programs written?
-
1Here is one related question: How do I get and modify the source code of packages installed through apt-get? – pa4080 Dec 04 '19 at 11:54
1 Answers
Where to find the code for the terminal commands?
Where to find it depends on the command. There are several locations.
Regarding the two you mention:
GNU coreutils
ls
is part of GNU Coreutils. You can get the source code for all the GNU core utilities (so not just for ls
: wikipedia list of core utilities) on www.gnu.org. From the command line using git (git
needs to be installed):
git clone git://git.sv.gnu.org/coreutils
The source for ls
can also be found online on git.savannah.gnu.org. All the other commands are accessible there too.
- Shell builtins
cd
is a built-in in the shell so is part of bash
. You can find the source code for bash on ftp.gnu.org. That will also be the source code for all builtins.
bash defines the following built-in commands: :, ., [, alias, bg, bind, break, builtin, case, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, if, jobs, kill, let, local, logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit, umask, unalias, unset, until, wait, while.
In what language are those programs written?
With that I would like to get into modifying existing programs and writing new ones
You are better off using the existing tools and adding to them: create a script called ls2
and then add functionality to that script to mimic ls
+ your additions. Your additions could be extra letters as parameters that trigger functions. Biggest advantage: you can write it in the language you prefer. Safer since you do not mess with the normal commands if your intention is to code new functionality for your these tools you should join their respective mailing lists.

- 299,756
-
Also, these programs are already compiled, so to modify them internally, it needs to be recompiled and replace current exist files (for example, if
ls
must displayls -l
by default, ...); I think also that it could be dangerous for the OS to do that – damadam Dec 04 '19 at 11:52 -
Nice answer! Thanks! Do you have a good resource for getting started with scripting ('ls2' example). And can I see some example scripts somewhere on my computer to mimic their behaviour? – Paul Erlenmeyer Dec 04 '19 at 20:21
-
not really :-P The
ls2
is just me being a coder and thinking on how -I- would approach this :D Like https://docs.python.org/3/library/configparser.html can be used to read an config file and argparse can be used to pass options to your script https://docs.python.org/3/library/argparse.htmlls2
could be a python script reading all the options and if those are part ofls
you print the results of the command to the display and if those are your new options you code something around it to then print the results. – Rinzwind Dec 04 '19 at 20:32 -
ah ok, thanks :). I know how to code with java and python. But don't you start python scripts with 'python' keyword and java with 'java -jar'? I mean in those cases even python or java needs to be installed on the machine. Maybe I just read the src links that you posted and find it out myself. – Paul Erlenmeyer Dec 05 '19 at 10:03
-
@PaulErlenmeyer You could add a python shebang to the beginning of your script, which would make it directly executable from the command line (as long as it's (a) in your
$PATH
and (b) executable [thinkchmod u+x
]) – MTCoster Dec 05 '19 at 15:22 -
not all GNU tools are written in C. One notable example is GCC which is written in C++. Scripting languages like perl, python or bash are also used a lot – phuclv Dec 05 '19 at 15:32
-
@MTCoster hehe, thought that something like that was the case. Thanks for the information :) – Paul Erlenmeyer Dec 05 '19 at 16:06
-
@phuclv GCC is not written completely in C++. According to their coding conventions, parts of the system must be in C. – doneal24 Dec 05 '19 at 17:34