Reputation:
I wanted to understand how exactly a bash terminal validates the commands that I enter and then gives me the output that I expect. Ex: When I enter "ls", how does it know that it is a valid command and how does it give me the list of the files? I'm not trying to understand "ls" command in particular but in general how these commands work.
Upvotes: 1
Views: 165
Reputation:
I'll try to explain this in a very simple way.
They're all binaries. They're run as regular processes. They have optional and/or mandatory command line arguments. They're present in one of the locations defined in $PATH environment variable. It is usually /usr/bin or /usr/sbin where you'll find those binaries.
Before looking into these locations, the OS first looks for bash aliases which are defined in .bashrc file. Bash aliases are like #defines in C. Read more about them here: https://mijingo.com/blog/creating-bash-aliases
But before even doing that, it looks for builtin commands i.e., commands which come by default with the shell. These are the most commonly used commands built into the shell's functionality so that the shell doesn't have to run a process each time these are called.
Ex: 'cd' is a shell builtin and 'ls' is not
~ # type cd
cd is a shell builtin
~ # type ls
ls is /bin/ls
Upvotes: 0
Reputation: 47
It lookups your $PATH env. Variable and recursively walks over PATH dirs to lookup binary file that matching your command. Like /bin/ls, for example
You can check your PATH by entering 'echo $PATH', or change it by 'export PATH=$PATH:/one/more/dir'
Also there are bash aliases, which you can setup to map some command to another one
Upvotes: 1