Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
6  
The set of bash built-ins is fixed, finite, and documented. Why do you need runtime checks? You could just test for bash version with something like BASH_VERSION or BASH_VERSINFO. –  jw013 Jul 9 '12 at 19:17
    
By “in bash”, do you mean builtins? For example, kill and set are builtins; bash and ls are not. –  Gilles Jul 9 '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 Jul 9 '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

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.