How to find out where alias (in the bash sense) is defined when running Terminal in Mac OS X

后端 未结 10 1097
时光取名叫无心
时光取名叫无心 2020-12-13 03:50

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).

相关标签:
10条回答
  • 2020-12-13 03:50

    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
    
    0 讨论(0)
  • 2020-12-13 03:50

    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 讨论(0)
  • 2020-12-13 03:56

    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

    0 讨论(0)
  • 2020-12-13 03:56

    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.

    0 讨论(0)
  • 2020-12-13 03:58

    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>

    0 讨论(0)
  • 2020-12-13 04:05

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

    or, as already mentioned above

    which name_of_alias

    0 讨论(0)
提交回复
热议问题