How do I create a Bash alias?

后端 未结 16 586
一个人的身影
一个人的身影 2020-11-28 17:38

I\'m on OSX and I need to put something like this, alias blah=\"/usr/bin/blah\" in a config file but I don\'t know where the config file is.

相关标签:
16条回答
  • 2020-11-28 18:06

    On OS X you want to use ~/.bash_profile. This is because by default Terminal.app opens a login shell for each new window.

    See more about the different configuration files and when they are used here: What's the difference between .bashrc, .bash_profile, and .environment?

    and in relation to OSX here: About .bash_profile, .bashrc, and where should alias be written in?

    0 讨论(0)
  • 2020-11-28 18:06

    In my .bashrc file the following lines were there by default:

    # Alias definitions.
    # You may want to put all your additions into a separate file like
    # ~/.bash_aliases, instead of adding them here directly.
    # See /usr/share/doc/bash-doc/examples in the bash-doc package.
    
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    

    Hence, in my platform .bash_aliases is the file used for aliases by default (and the one I use). I'm not an OS X user, but I guess that if you open your .bashrc file, you'll be able to identify what's the file commonly used for aliases in your platform.

    0 讨论(0)
  • 2020-11-28 18:07

    I just open zshrc with sublime, and edit it.

    subl .zshrc
    

    And add this on sublime:

    alias blah="/usr/bin/blah"
    

    Run this in terminal:

    source ~/.bashrc
    

    Done.

    0 讨论(0)
  • 2020-11-28 18:07

    MacOS Catalina and Above

    Apple switched their default shell to zsh, so the config files include ~/.zshenv and ~/.zshrc. This is just like ~/.bashrc, but for zsh. Just edit the file and add what you need; it should be sourced every time you open a new terminal window:

    nano ~/.zshenv alias py=python

    Then do ctrl+x, y, then enter to save.

    This file seems to be executed no matter what (login, non-login, or script), so seems better than the ~/.zshrc file.

    High Sierra and earlier

    The default shell is bash, and you can edit the file ~/.bash_profile and add aliases:

    nano ~/.bash_profile alias py=python

    Then ctrl+x, y, and enter to save. See this post for more on these configs. It's a little better to set it up with your alias in ~/.bashrc, then source ~/.bashrc from ~/.bash_profile. In ~/.bash_profile it would then look like:

    source ~/.bashrc

    0 讨论(0)
  • 2020-11-28 18:10
    1. Go to home
    2. Open .bashrc
    3. Create alias at bottom of the file

      alias alias_name='command to do'
      eg: alias cdDesktop='cd /Desktop'
      
    4. Save the file

    5. source .bashrc

      source ~/.bashrc
      
    6. Open terminal (Ctrl+Alt+T) & type cdDesktop & press enter

    0 讨论(0)
  • 2020-11-28 18:10

    I need to run the Postgres database and created an alias for the purpose. The work through is provided below:

    $ nano ~/.bash_profile 
    
    # in the bash_profile, insert the following texts:
    
    alias pgst="pg_ctl -D /usr/local/var/postgres start"
    alias pgsp="pg_ctl -D /usr/local/var/postgres stop"
    
    
    $ source ~/.bash_profile 
    
    ### This will start the Postgres server 
    $ pgst
    
    ### This will stop the Postgres server 
    $ pgsp
    
    0 讨论(0)
提交回复
热议问题