0

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.

Sachin
  • 169
  • 1
  • 2
  • 6
  • 7
    The set of `bash` built-ins is fixed, finite, and [documented](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Builtin-Commands). Why do you need runtime checks? You could just test for `bash` version with something like `BASH_VERSION` or `BASH_VERSINFO`. – jw013 Jul 09 '12 at 19:17
  • By “in bash”, do you mean builtins? For example, `kill` and `set` are builtins; `bash` and `ls` are not. – Gilles 'SO- stop being evil' Jul 09 '12 at 22:06
  • 3
    @jw013 I thought Sachin might be trying to distinguish between standard commands such as `ls` 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:15
  • @Gilles: you put it right I am trying to distinguish between any custom script written by a user and any other command that we run from bash. The command can be builtins or as you pointed out `ls` and `bash` are not `builtins` but I will lick to consider them as valid and any script as invalid – Sachin Jul 10 '12 at 14:03
  • 1
    @Sachin You'll have to define more specifically what you consider invalid (or what you want to exclude). Would a script placed in `/usr/bin` by root be considered a user script? What about a binary installed into `/usr/local/bin`? Would `command -p`/setting `PATH` to some default value suffice? – jw013 Jul 10 '12 at 17:05
  • Yes any script written by the user would be valid but not any script written by the user. I did not understand what you mean by `command -p/setting PATH to some default value` – Sachin Jul 10 '12 at 18:59
  • @Sachin: "any script written by the user would be valid but not any script written by the user" seems contradictory to me –  Sep 17 '12 at 13:41
  • (Really it's a duplicate of [How to test if a command is installed?](http://unix.stackexchange.com/q/273955/135943), but that's a duplicate of the question linked above, albeit a non-obvious dupe.) – Wildcard Apr 03 '16 at 06:51
  • [How to check if a program exists from a Bash script?](https://stackoverflow.com/q/592620/608639) –  Mar 26 '19 at 02:57

1 Answers1

11

Not sure about returning true for just a shell builtin, but type -t will return the type of 'command', including "builtin":

if [ "$(type -t help)" = "builtin" ]; then
    echo "Help is a builtin command."
fi

Run help type for more information.

phemmer
  • 70,657
  • 19
  • 188
  • 223
Arcege
  • 22,287
  • 5
  • 56
  • 64