How do I create a Bash alias?

2019-01-09 23:33发布

问题:

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.

回答1:

You can add an alias or a function in your startup script file. Usually this is .bashrc, .bash_login or .profile file in your home directory.

Since these files are hidden you will have to do an ls -a to list them. If you don't have one you can create one.


If I remember correctly, when I had bought my Mac, the .bash_login file wasn't there. I had to create it for myself so that I could put prompt info, alias, functions, etc. in it.

Here are the steps if you would like to create one:

  1. Start up Terminal
  2. Type cd ~/ to go to your home folder
  3. Type touch .bash_profile to create your new file.
  4. Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
  5. Type . .bash_profile to reload .bash_profile and update any alias you add.


回答2:

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?



回答3:

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.



回答4:

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.



回答5:

cd /etc
sudo vi bashrc

Add the following like:

alias ll="ls -lrt"

Finally restart Terminal.



回答6:

The config file for scripts and programs is ~/.bashrc and the config file that gets loaded when you use Terminal is ~/.bash_login.

I think the best way is to just have everything in ~/.bashrc.

For your specific question just enter (this will overwrite any existing ~/.bashrc):

echo "alias blah=\"/usr/bin/blah\"" >>~/.bashrc

into the Terminal and a ~/.bashrc file will be created with your new alises. After that just edit the file to add new aliases, functions, settings etc.



回答7:

  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



回答8:

If you put blah="/usr/bin/blah" in your ~/.bashrc then you can use $blah in your login shell as a substitute for typing /usr/bin/blah



回答9:

You probably want to edit the .bashrc file in your home directory.



回答10:

To create permanent alias shortcut, put it in .bash_profile file and point .bashrc file to .bash_profile file. Follow these steps (I am creating an alias command called bnode to run babel transpiler on ES6 code):

  1. Go to terminal command prompt and type “cd” (this will take you to home directory. Note: even though your programming files may be located on your “D: drive”, your “.bash” files may be located on your “C: drive” )
  2. To see location of home directory, type “pwd” (this will show you the home directory path and where the .bash files are probably located)
  3. To see all dot "." files in home directory, type “ls -la” (this will show ALL files including hidden dot "." files)
  4. You will see 2 files: “.bash_profile” and “.bashrc”
  5. Open .bashrc file in VS Code Editor or your IDE and enter “source ~/.bash_profile” in first line (to point .bashrc file to .bash_profile)
  6. Open .bash_profile file in VS Code Editor and enter “alias bnode='./node_modules/.bin/babel-node'” (to create permanent bnode shortcut to execute as bash command)
  7. Save and close both files
  8. Now open the file you want to execute (index.js) and open in terminal command prompt and run file by using command “bnode index.js”
  9. Now your index.js file will execute but before creating bnode alias in .bash_profile file you would get the error "bash: bnode command not found" and it would not recognize and give errors on some ES6 code.
  10. Helpful link to learn about dotfiles: https://dotfiles.github.io/

Hope this helps! Good luck!



标签: macos bash shell