Notes on the ‘type’ Command in Bash — Not Something you’d Expect coming from Python and Powershell.

The aim of this page📝is to present the type command in bash, noting there is no object model in bash that one may take for granted coming from Python & Powershell.

  • note that type in bash does not tell you the type of variable you have - like type(<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 posh/python then, a weird thing may happen
<!-- PYTHON -->
>>> greeting = "hello"
>>> type(greeting)
<class 'str'>

<!-- POSH -->
>>> $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” that the type command returns
  1. alias → command is shell alias
  2. keyword → command is shell reserved word
  3. function → command is shell function
  4. builtin → command is shell builtin
  5. file → command is disk file
  • if it is a function
$ 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 posh, type is a built-in alias for Get-Content aka cat command

--

--

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com