Find How a Bash Command will be Interpreted

  • HOME
  • >
  • BASH
  • >
  • Find How a Bash Command will be Interpreted
Last Updated: 

You may have set some personnal alias or function in your profile .bashrc. The most common ones are probably dir, ls, ll, etc. In the event you forgot how you defined your alias or function, the easiest way for get back the definition isn't to read your numerous .bashrc but by using the bash builtin command : type or command.

type: usage: type [-afptP] name [name ...]
command: usage: command [-pVv] command [arg ...]

command and type are very similar and can both indicate how your command or alias would be interpreted if used as a command name. See some examples below. Also, to find out all your currently defined aliases - alias only - simply run alias -p.

[root@host ~]$ alias -p
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
[root@host ~]$ command -v rm
alias rm='rm -i'
[root@host ~]$ command -V rm
rm is aliased to `rm -i'
[root@host ~]# type rm
rm is aliased to `rm -i'

Another example to compare type, command -v, and command -V wiusingth a readlink alias and a custom function for Docker images clean-up.

# .bashrc
alias readlink="greadlink"
dcleanup(){
    docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
    docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}

[root@host ~]$ type readlink
readlink is aliased to `greadlink'
[root@host ~]$ type dcleanup
dcleanup is a function
dcleanup () 
{ 
    docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2> /dev/null;
    docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2> /dev/null
}


[root@host ~]$ command -v readlink
alias readlink='greadlink'
[root@host ~]$ command -v dcleanup
dcleanup

[root@host ~]$ command -V readlink
readlink is aliased to `greadlink'
[root@host ~]$ command -V dcleanup
dcleanup is a function
dcleanup () 
{ 
    docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2> /dev/null;
    docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2> /dev/null
}

👉 If you need a refresher on aliases, check the post Working with Bash Aliases (Alias/Unalias)

Related bash posts that you may like
What is the Bash Null Command?
Learn about the Bash null command, also known as the POSIX shell colon command. This post cover concrete use cases and pitfalls to avoid.
How To Format Date and Time in Linux, macOS, and Bash?
Find out how to manipulate date and time on linux and macOS systems as well as natively in the Bash shell. This post covers all you need to know to format a date from your shell.
How To Use Option as Meta Key in macOS Terminal?
The Meta Key is a modifier key that can be quite helpful to improve your productivity while working in a terminal and bash. This post cover how to enable from the command line the Meta Key in macOS Terminal.