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.
bash
built-ins is fixed, finite, and documented. Why do you need runtime checks? You could just test forbash
version with something likeBASH_VERSION
orBASH_VERSINFO
. – jw013 Jul 09 '12 at 19:17kill
andset
are builtins;bash
andls
are not. – Gilles 'SO- stop being evil' Jul 09 '12 at 22:06ls
and 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:15ls
andbash
are notbuiltins
but I will lick to consider them as valid and any script as invalid – Sachin Jul 10 '12 at 14:03/usr/bin
by root be considered a user script? What about a binary installed into/usr/local/bin
? Wouldcommand -p
/settingPATH
to 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