I want to check if a given command exists in bash and I care only about the native commands of the bash and not the scripts written by the user. When I refer to native commands I mean all those commands that are generally available to a user.
There are two commands
command -v
type
that can be used to check if the command exists. But it also returns a true for user written scripts whereas I want to restrict myself just to the inbuilt commands.
Note: I am not just trying to distinguish between a bash builtin and other commands because
type -t touch
gives file as the type, but this is a command available to the user by default. But if the user writes a script and then tries to run it then I would like to filter that.
Any help is appreciated.
bashbuilt-ins is fixed, finite, and documented. Why do you need runtime checks? You could just test forbashversion with something likeBASH_VERSIONorBASH_VERSINFO. – jw013 Jul 09 '12 at 19:17killandsetare builtins;bashandlsare not. – Gilles 'SO- stop being evil' Jul 09 '12 at 22:06lsand custom scripts. In which case the answer is that there's no such distinction. Sachin, if that's what you were asking, there may be a way to do whatever you're trying to do, but you should tell us what you're trying to accomplish without focusing on a dommed approach. – Gilles 'SO- stop being evil' Jul 09 '12 at 23:15lsandbashare notbuiltinsbut I will lick to consider them as valid and any script as invalid – Sachin Jul 10 '12 at 14:03/usr/binby root be considered a user script? What about a binary installed into/usr/local/bin? Wouldcommand -p/settingPATHto some default value suffice? – jw013 Jul 10 '12 at 17:05command -p/setting PATH to some default value– Sachin Jul 10 '12 at 18:59