Determine where a UNIX alias is defined

Is there a (somewhat) reliable way to get the "start" of a command, even if the command is an alias? For example, if I put this in my .bash_profile

alias lsa="ls -A" 

and I wanted to know from the command line where lsa defined, is this possible? I know about which command, but that doesn't seem to do it.

+7
source share
3 answers

As Carl noted in his comment, type is the right way to find out how a name is defined.

 mini:~ michael$ alias foo='echo bar' mini:~ michael$ biz() { echo bar; } mini:~ michael$ type foo foo is aliased to `echo bar' mini:~ michael$ type biz biz is a function biz () { echo bar } mini:~ michael$ type [[ [[ is a shell keyword mini:~ michael$ type printf printf is a shell builtin mini:~ michael$ type $(type -P printf) /usr/bin/printf is /usr/bin/printf 
+15
source

While type and which tell you about the source, they are not looking for a few steps. I wrote a small program for this: origin . Example:

 alext@smith :~/projects/origin$ ./origin ll 'll' is an alias for 'ls' in shell '/bin/bash': 'ls -alF' 'ls' is an alias for 'ls' in shell '/bin/bash': 'ls --color=auto' 'ls' found in PATH as '/bin/ls' '/bin/ls' is an executable alext@smith :~/projects/origin$ 
+1
source

This function will provide information about what type of command:

 ft () { t="$(type -t "$1")"; if [ "$t" = "file" ]; then if which -s "$1"; then file "$(which "$1")" else return 1 fi else echo $t fi return 0 } 

It will either spit out builtin , alias , etc., a line like /bin/ls: Mach-O 64-bit x86_64 executable if the file, or nothing if not. It will return an error in the latter case.

0
source

All Articles