How do I create a Bash alias?

后端 未结 16 588
一个人的身影
一个人的身影 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:12

    To create a 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 the 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 the location of the 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 the 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/

    I hope this helps! Good luck!

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

    It works for me on macOS Majave

    You can do a few simple steps:

    1) open terminal

    2) sudo nano /.bash_profile

    3) add your aliases, as example:

    # some aliases
    alias ll='ls -alF'
    alias la='ls -A'
    alias eb="sudo nano ~/.bash_profile && source ~/.bash_profile"
    #docker aliases
    alias d='docker'
    alias dc='docker-compose'
    alias dnax="docker rm $(docker ps -aq)"
    #git aliases
    alias g='git'
    alias new="git checkout -b"
    alias last="git log -2"
    alias gg='git status'
    alias lg="git log --pretty=format:'%h was %an, %ar, message: %s' --graph"
    alias nah="git reset --hard && git clean -df"
    alias squash="git rebase -i HEAD~2"
    

    4) source /.bash_profile

    Done. Use and enjoy!

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

    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.

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

    For macOS Catalina Users:

    Step 1: create or update .zshrc file

    vi ~/.zshrc
    

    Step 2: Add your alias line

    alias blah="/usr/bin/blah"
    

    Step 3: Source .zshrc

    source ~/.zshrc 
    

    Step 4: Check you're alias, by typing alias on the command prompt

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

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

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

    create a bash_profile at your user root - ex

    /user/username/.bash_profile
    

    open file

    vim ~/.bash_profile

    add alias as ex. (save and exit)

    alias mydir="cd ~/Documents/dirname/anotherdir"
    

    in new terminal just type mydir - it should open

    /user/username/Documents/dirname/anotherdir
    
    0 讨论(0)
提交回复
热议问题