Explaining `type` in Bash
The aim of this pageđź“ťis to present the type
command in bash. Bash has no types. No objects. Nothing like anobject model in bash — keep that in mind if coming from Python or Powershell. But the type
command exists! There are five types — 4 command types (aliases, functions, files, and ~60 shell built-ins) + keyword (~20).
For each name, indicate how it would be interpreted if used as a command name.
The above is the definition of the type
command from
— https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-type
1.1. type
indicates a type of command — not not as a variable!
- note that
type
in bash does not tell you the type of variable you have - liketype(<name>)
in Python
Bash doesn’t have types in the same way as Python (although I would say that Python has classes rather than types). But bash variables do have attributes that are given (mostly) through declare, but the range of attributes is fairly small. You can find an attribute using declare -p, for example, declare -i creates an integer
— https://stackoverflow.com/a/29840856/11082684
- coming from Powershell/python then, a weird thing may happen
# python
>>> greeting = "hello"
>>> type(greeting)
<class 'str'>
# powershell
>>> $greeting = "hello"
>>> $greeting.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
# bash
>>> greeting="hello"
>>> echo $greeting
hello
>>> type $greeting
-bash: type: hello: not found
- there are only 5 possible “types” of commands that the
type
command returns - 4/5 are executable and indeed function as commands;
keyword
is special - NOTE: there is an order in which types are prioritized — see the table below
- This means that you can overwrite the default type with an alias and it has priority. Also, builtin has a priority over file
$ prnt(){
> echo "you passed me" $*
> }
$ prnt fd fdsfsd fs fdsf
you passed me fd fdsfsd fs fdsf
$ type prnt
prnt is a function
prnt ()
{
echo "you passed me" $*
}
- in PowerShell,
type
is a built-in alias forGet-Content
akacat
command