Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking in bash and csh if a command is builtin

Tags:

How can I check in bash and csh if commands are builtin? Is there a method compatible with most shells?

like image 771
Natan Yellin Avatar asked Sep 13 '11 09:09

Natan Yellin


People also ask

How do you check if a command is built-in or not?

Use type -a , which gives all occurrences of the command name, including whether it's a built-in. Then grep to see if one of the lines says that it's a built-in.

How check if command is built-in Bash?

Use the command -v Command to Check if a Command Exists in Bash. The command -v is a built-in function in all POSIX systems and Bash. This function checks if a command exists as it returns the valid path for that command if it does exist and returns NULL if it does not.

Is a command that is built-in to the shell?

In computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute. Shell builtins work significantly faster than external programs, because there is no program loading overhead.


1 Answers

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 echo
echo: shell built-in command.

# which parted
/sbin/parted

In bash:

# type echo
echo is a shell builtin

# type parted
parted is /sbin/parted

type might also show something like this:

# type clear
clear 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.

like image 68
Jon Lin Avatar answered Sep 19 '22 11:09

Jon Lin