91

How can I find out where an alias is defined on my system? I am referring to the kind of alias that is used within a Terminal session launched from Mac OS X (10.6.3).

For example, if I enter the alias command with no parameters at a Terminal command prompt, I get a list of aliases that I have set, for example:

alias mysql='/usr/local/mysql/bin/mysql'

However, I have searched all over my system using Spotlight and mdfind in various startup files and so far can not find where this alias has been defined. ( I did it a long time ago and didn't write down where I assigned the alias).

2

12 Answers 12

77

For OSX, this 2-step sequence worked well for me, in locating an alias I'd created long ago and couldn't locate in expected place (~/.zshrc).

cweekly:~ $ which la
la: aliased to ls -lAh

cweekly:~$ grep -r ' ls -lAh' ~
/Users/cweekly//.oh-my-zsh/lib/aliases.zsh:alias la='ls -lAh'

Aha! "Hiding" in ~/.oh-my-zsh/lib/aliases.zsh. I had poked around a bit in .oh-my-zsh but had overlooked lib/aliases.zsh.

7
  • Thank you for this! May 15, 2017 at 0:59
  • 1
    This is brute force but effective and doesn't require any guesswork. Seems like the easiest way to go.
    – jcollum
    Oct 2, 2017 at 18:05
  • 2
    @lacostenycoder you may have omitted the ~ argument at the end of the command, without it grep will wait to parse any input rather than searching across files locally.
    – Peemster
    Mar 4, 2020 at 16:16
  • 1
    This actually the exact alias I was looking for, with the same culprit Oh-my-zsh. Thanks
    – Brenwell
    Oct 28, 2020 at 5:06
  • 1
    FWIW, this ls -lAh alias looks to be in ~/.oh-my-zsh/lib/directories.zsh now.
    – loeschg
    Apr 28, 2022 at 14:56
38

you can just simply type in alias on the command prompt to see what aliases you have. Otherwise, you can do a find on the most common places where aliases are defined, eg

grep -RHi "alias" /etc /root
1
  • 4
    Options: -R, -r, --recursive: Recursively search subdirectories listed, -H: Always print filename headers with output lines, -i, --ignore-case: Perform case insensitive matching. By default, grep is case sensitive.
    – Hugo
    Feb 25, 2019 at 12:10
31

First use the following commands

List all functions

functions 

List all aliases

alias 

If you aren't finding the alias or function consider a more aggressive searching method

Bash version

bash -ixlc : 2>&1 | grep thingToSearchHere

Zsh version

zsh -ixc : 2>&1 | grep thingToSearchHere

Brief Explanation of Options

-i     Force shell to be interactive.

-c     Take the first argument as a command to execute

-x      -- equivalent to --xtrace

-l      Make bash act as if invoked as a login shell
5
  • What does : after -ixc mean, is it a command to be executed? Thanks!
    – Leo
    Apr 8, 2020 at 2:40
  • 1
    : is a shell builtin that is basically equivalent to the true command. Historically, Bourne shells didn't have true and false as built-in commands. Apr 8, 2020 at 20:34
  • 1
    @Chad see above Apr 8, 2020 at 20:35
  • This is great! For me trying to find which file in zsh I did zsh -ixc : 2>&1 | ag $pattern | grep '/' | grep alias: after defining my pattern string pattern=someLongThing Jan 27, 2021 at 20:35
  • Always appreciate it when someone appreciates the help (tips hat) Jan 27, 2021 at 20:43
19

Also in future these are the standard bash config files

  • /etc/profile
  • ~/.bash_profile or ~/.bash_login or ~/.profile
  • ~/.bash_logout
  • ~/.bashrc

More info: http://www.heimhardt.com/htdocs/bashrcs.html

3
  • 3
    /etc/bashrc is also be looked at. Feb 2, 2013 at 11:12
  • 2
    On some machines /etc/bashrc may be named /etc/bash.bashrc. It is on my Debian 7 machine anyway. Jan 5, 2015 at 19:14
  • 1
    In addition, some distros put system wide settings in /etc/bash/. Jan 22, 2016 at 8:02
8

A bit late to the party, but I was having the same problem (trying to find where the "l." command was aliased in RHEL6), and ended up in a place not mentioned in the previous answers. It may not be found in all bash implementations, but if the /etc/profile.d/ directory exists, try grepping there for unexplained aliases. That's where I found:

[user@server ~]$ grep l\\. /etc/profile.d/*
/etc/profile.d/colorls.csh:alias l. 'ls -d .*'
/etc/profile.d/colorls.csh:alias l. 'ls -d .* --color=auto'
/etc/profile.d/colorls.sh:  alias l.='ls -d .*' 2>/dev/null
/etc/profile.d/colorls.sh:alias l.='ls -d .* --color=auto' 2>/dev/null

The directory isn't mentioned in the bash manpage, and isn't properly part of where bash searches for profile/startup info, but in the case of RHEL you can see the calling code within /etc/profile:

for i in /etc/profile.d/*.sh ; do
  if [ -r "$i" ]; then
    if [ "${-#*i}" != "$-" ]; then
      . "$i"
    else
      . "$i" >/dev/null 2>&1
    fi
  fi
done
3

Please do check custom installations/addons/plugins you have added, in addition to the .zshrc/.bashrc/.profile etc files

So for me: it was git aliased to 'g'.

$ which g
g: aliased to git

Then I ran the following command to list all aliases

$ alias

I found a whole lot of git related aliases that I knew I had not manually added. This got me thinking about packages or configurations I had installed. And so went to the

.oh-my-zsh directory. Here I ran the following command:

$ grep -r 'git' . |grep -i alias

And lo and behold, I found my alias in :

./plugins/git/git.plugin.zsh

1

I found the answer ( I had been staring at the correct file but missed the obvious ).

The aliases in my case are defined in the file ~/.bash_profile

Somehow this eluded me.

1
  • thank you, tried .bashrc and .bash_aliases, forgot it was .bash_profile
    – neaumusic
    Sep 10, 2015 at 18:14
1

For more complex setups (e.g. when you're using a shell script framework like bash-it, oh-my-zsh or the likes) it's often useful to add 'alias mysql' at key positions in your scripts. This will help you figure out exactly when the alias is added.

e.g.:

echo "before sourcing .bash-it:"
alias mysql
. $HOME/.bash-it/bash-it.sh
echo "after sourcing bash:"
alias mysql
0

I think that maybe this is similar to what ghostdog74 meant however their command didn't work for me.

I would try something like this:

for i in `find . -type f`; do   # find all files in/under current dir
echo "========" 
echo $i                         # print file name
cat $i | grep "alias"           # find if it has alias and if it does print the line containing it
done

If you wanted to be really fancy you could even add an if [[ grep -c "alias" ]] then <print file name>

1
  • 3
    Rather than loop, backtick, find, echo, and cat, a simple grep -R alias . will do what you suggest. The crux of the question however is where to look.
    – dimo414
    Sep 26, 2013 at 20:23
0

The only reliable way of finding where the alias could have been defined is by analyzing the list of files opened by bash using dtruss.

If

$ csrutil status
System Integrity Protection status: enabled.

you won't be able to open bash and you may need a copy.

$ cp /bin/bash mybash
$ $ codesign --remove-signature mybash

and then use

sudo dtruss -t open ./mybash -ic exit 2>&1 | awk -F'"' '/^open/ {print substr($2, 0, length($2)-2)}'

to list all the files where the alias could have been defined, like

/dev/dtracehelper
/dev/tty
/usr/share/locale/en_CA.UTF-8/LC_MESSAGES/BASH.mo
/usr/share/locale/en_CA.utf8/LC_MESSAGES/BASH.mo
/usr/share/locale/en_CA/LC_MESSAGES/BASH.mo
/usr/share/locale/en.UTF-8/LC_MESSAGES/BASH.mo
/usr/share/locale/en.utf8/LC_MESSAGES/BASH.mo
/usr/share/locale/en/LC_MESSAGES/BASH.mo
/Users/user/.bashrc
/Users/user/.bash_aliases
/Users/user/.bash_history
...
-5

Try: alias | grep name_of_alias Ex.: alias | grep mysql

or, as already mentioned above

which name_of_alias

-8

In my case, I use Oh My Zsh, so I put aliases definition in ~/.zshrc file.

3
  • Posted with more detail two and a half years ago. Apr 6, 2017 at 3:56
  • The question is that he can't remember where aliases are defined in Mac OSX which I had run into, so I just give the file location(~/.zshrc) that worked for me(I use Oh My Zsh). I really don't know what do you mean about more detail.
    – ryan
    Apr 6, 2017 at 4:39
  • 2
    I mean, an existing answer from Nov 2014 already said everything in this answer and added more as well. Apr 6, 2017 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.