Checking in bash and csh if a command is builtin
You can try using which
in csh or type
in bash. If something is a built-in command, it will say so; otherwise, you get the location of the command in your PATH.
In csh:
# which echoecho: shell built-in command.# which parted/sbin/parted
In bash:
# type echoecho is a shell builtin# type partedparted is /sbin/parted
type
might also show something like this:
# type clearclear is hashed (/usr/bin/clear)
...which means that it's not a built-in, but that bash has stored its location in a hashtable to speed up access to it; (a little bit) more in this post on Unix & Linux.
In bash, you can use the type
command with the -t
option. Full details can be found in the bash-builtins
man page but the relevant bit is:
type -t name
If the
-t
option is used, type prints a string which is one ofalias
,keyword
,function
,builtin
, orfile
if name is an alias, shell reserved word, function, builtin, or disk file, respectively. If the name is not found, then nothing is printed, and an exit status of false is returned.
Hence you can use a check such as:
if [[ "$(type -t read)" == "builtin" ]] ; then echo read ; fiif [[ "$(type -t cd)" == "builtin" ]] ; then echo cd ; fiif [[ "$(type -t ls)" == "builtin" ]] ; then echo ls ; fi
which would result in the output:
readcd