I am trying to write a tiny shell. How can I distinguish that if an input argument is a builtin command (if so, I will fork and execute it)?
PS. I am working on Ubuntu 18.04 LTS.
Asked
Active
Viewed 505 times
3

TianpingHsu
- 91
- 2
- 5
-
Possible duplicate of [How to check if command exists in a shell script?](https://stackoverflow.com/questions/7522712/how-to-check-if-command-exists-in-a-shell-script) – kiran Biradar Aug 06 '18 at 08:28
-
4You're writing a shell, _you_ decide what utilities to implement as builtins (except for the few that are _required_ to be builtins, check the POSIX standard). – Kusalananda Aug 06 '18 at 08:44
1 Answers
3
Well, if you are writing your own shell, then technically it is up to your implementation what is and isn't a built in command.
For builtins
In bash, ksh etc. you can use compgen -b to generate a list of builtins in corresponding shell.
-A action
...
builtin
Names of shell builtin commands. May also be specified as -b.
For non builtins
For non-builtins search the path, or refer to FHS and search e.g. /bin /sbin /usr/bin /usr/sbin.

visibleman
- 3,175
- 1
- 14
- 27
-
Thank you. I reread the guide and I think you are right, it is up to me. – TianpingHsu Aug 06 '18 at 08:58
-
Also take note of @kusalananda, in that the there are certain built-ins that you are required to provide to be POSIX compliant. – visibleman Aug 06 '18 at 09:08