How to make alias mac os

Make an Alias in Bash or Zsh Shell in macOS with Terminal

To make aliases of macOS Unix commands in your bash or zsh shell on macOS and earlier versions, it is done via your .bash_profile or .zshrc file which lives in your home account directory, if the file does not already exist, just create one.

Launch Terminal from the /Application/Utilities folder

Go to your home directory by just entering ‘cd’ followed by the ‘return’ key to enter the command:

List your home directory contents including invisible files to see if the file already exists, use:

Create the .bash_profile or .zshrc file using the command line program called ‘nano’ if it doesn’t exist, if it does exist you add your aliases to the end of the file. The commands below assume you are in your home directory:

If you are not in your home directory you would use a prefix of the tilde ‘

’ and forward slash, which is the home directory path:

When the .bash_profile or .zshrc file is created you are ready to enter your alias commands.
So here I am using the alias ‘l’ to alias the command ‘ls -lah’

In nano ‘control+o’ to write the file out and ‘control+x’ to exit the file.

Refresh the shell environment by entering the command below:

That’s it, now the alias will take effect.

To add other aliases just start a new line, and apply the same formatting.

Источник

Question: Q: Using aliases in Terminal

I’m trying to add new alias to my terminal so I used the below commands

First I went to my bash_profile by using the commands

nano .bash_profile

Then I added the below line at the end of the file and saved it & exit the file

alias htdocs= «cd /Applications/mamp/htdocs»

After that I closed the terminal and opened it again to restart it, then I typed «htdocs» then pressed enter but still found the terminal informing me «-sh: htdocs: command not found «

Now I’m asking why the alias not working with me . , what I miss .

MacBook Pro (Retina, 13-inch, Mid 2014), OS X Mavericks (10.9.5)

Posted on Feb 28, 2018 5:46 AM

All replies

Loading page content

Page content loaded

It has been years since I used MAMP, so I don’t know if your path to htdocs is correct. Isn’t MAMP capitalized?

When you update the

/.bashrc file, you either update your Bash session by quitting the Terminal and restarting it, or with the following command which updates your alias without quitting Terminal:

Also, I use single, straight quotes on my alias target.

Feb 28, 2018 6:13 AM

After I used the command source

/.bash_profile , I found it working well now

Feb 28, 2018 6:23 AM

That would imply that you did not quit Terminal and re-launch it after modifying the .bash_profile

Of that you have some funky Terminal preferences set that is preventing .bash_profile from being read by the shell when you start.

I’m assuming you are running the default shell ‘bash’

If you are running a different shell, that also might explain it.

Feb 28, 2018 6:32 AM

I found that usually after closing the terminal or restarting my mac I found the alias undefined and the terminal informing me » -sh: htdocs: command not found «
what I should do to keeping it for every .

Mar 3, 2018 11:50 PM

1 — I put this line in the being of my .profile_bash

/».bash_profile but claims to be $0 running under $«

I put similar lines in all my profiles.

here is what I see when I start a terminal session.

Yes, my .bash_profile calls config/profile.bash. The /etc/profile and /etc/bashrc are system stuff.

2 — run the alias command to see what was aliased.

3 — put an echo before and after your alias command.

4— put the alias command alone «alias» after your aliasing to see if your command worked.

Читайте также:  Служба удаленного управления mac os

Doing this will help debug your system.

Mar 4, 2018 11:18 AM

You are NOT running ‘bash’, so .bash_profile is not being invoked

the -sh says you are running sh. If you were running bash it would say -bash : but it does not.

What does the following command say:

If the output is

then you need to use .profile

Or you need to change your shell to the default bash.

It is possible that the dscl command will say /bin/bash. If that is the case, then it is possible you have your Terminal -> Preferences -> General -> Shells to open with: (X) Command (complete path): /bin/sh

Mar 4, 2018 1:20 PM

Dears Guys,
First I would like every one for helping me, It’s working well now after I changed the alias to .profile as BobHarris saidto me

Thanks for every one

Mar 5, 2018 12:09 AM

First I would like every one for helping me, It’s working well now after I changed the alias to .profile as BobHarris saidto me

Some information to keep in the back of your mind.

By default macOS sets the default shell to bash. The fact that you are using sh means there has been some change made to your macOS setup. Whether you did this intentionally, or by accident, you should keep this in mind.

And the reason you should keep this in mind is that if you create a new account, or switch to a different Mac, you may find that in the new environment bash is now your shell, and assumptions you were making in your current environment are not working the same in the new environment.

And now that you have BOTH a .bash_profile _AND_ a .profile, you should know that if you magically switch back to bash, that when bash starts up it looks for one of the following 3 initialization files in the following order, and will use the first it finds, then ignore all the others

I mention this, because if you end up back with bash in your current account, it will start using the .bash_profile you currently have, and ignore the .profile you may have built up. This will be confusing, so keep this bit of information in the back of your mind. Unless of course you delete the .bash_profile, then bash will also find your .profile.

Источник

How do I create a Bash alias?

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.

/.bash_profile to my bash_profile and then I source it and run addAlias hi ‘echo «hi» ‘ . Source bash profile and type hi to see it.

16 Answers 16

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

  • Type touch .bash_profile to create your new file.
  • Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
  • Type . .bash_profile to reload .bash_profile and update any alias you add.
  • I just open zshrc with sublime, and edit it.

    And add this on sublime:

    Run this command in terminal:

    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?

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

    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.

    MacOS Catalina and Above

    Apple switched their default shell to zsh, so the config files include

    /.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:

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

    High Sierra and earlier

    The default shell is bash, and you can edit the file

    /.bash_profile and add aliases:

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

    /.bash_profile it would then look like:

    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:

    4) source /.bash_profile

    Done. Use and enjoy!

    Add the following like:

    Finally restart Terminal.

    /.bash_profile (command which activates/ reloads the bash aliases)

    The config file for scripts and programs is

    /.bashrc and the config file that gets loaded when you use Terminal is

    I think the best way is to just have everything in

    For your specific question just enter (this will overwrite any existing

    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.

    Create alias at bottom of the file

    Open terminal (Ctrl+Alt+T) & type cdDesktop & press enter

    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

    For macOS Catalina Users:

    Step 1: create or update .zshrc file

    Step 2: Add your alias line

    Step 3: Source .zshrc

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

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

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

    create a bash_profile at your user root — ex

    add alias as ex. (save and exit)

    in new terminal just type mydir — it should open

    I think it’s proper way:

    1) Go to teminal. open

    /.bashrc . Add if not exists

    /.bash_aliases . If not exists: touch

    3) To add new alias rather
    — edit .bash_aliases file and restart terminal or print source

    /.bash_aliases
    — print echo «alias clr=’clear'» >>

    /.bash_aliases where your alias is alias clr=’clear’ .

    4) Add line source

    /.bash_profile file. It needs to load aliases in each init of terminal.

    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

    Источник

    Question: Q: Using aliases in Terminal

    I’m trying to add new alias to my terminal so I used the below commands

    First I went to my bash_profile by using the commands

    nano .bash_profile

    Then I added the below line at the end of the file and saved it & exit the file

    alias htdocs= «cd /Applications/mamp/htdocs»

    After that I closed the terminal and opened it again to restart it, then I typed «htdocs» then pressed enter but still found the terminal informing me «-sh: htdocs: command not found «

    Now I’m asking why the alias not working with me . , what I miss .

    MacBook Pro (Retina, 13-inch, Mid 2014), OS X Mavericks (10.9.5)

    Posted on Feb 28, 2018 5:46 AM

    All replies

    Loading page content

    Page content loaded

    It has been years since I used MAMP, so I don’t know if your path to htdocs is correct. Isn’t MAMP capitalized?

    When you update the

    /.bashrc file, you either update your Bash session by quitting the Terminal and restarting it, or with the following command which updates your alias without quitting Terminal:

    Also, I use single, straight quotes on my alias target.

    Feb 28, 2018 6:13 AM

    After I used the command source

    /.bash_profile , I found it working well now

    Feb 28, 2018 6:23 AM

    That would imply that you did not quit Terminal and re-launch it after modifying the .bash_profile

    Of that you have some funky Terminal preferences set that is preventing .bash_profile from being read by the shell when you start.

    I’m assuming you are running the default shell ‘bash’

    If you are running a different shell, that also might explain it.

    Feb 28, 2018 6:32 AM

    I found that usually after closing the terminal or restarting my mac I found the alias undefined and the terminal informing me » -sh: htdocs: command not found «
    what I should do to keeping it for every .

    Mar 3, 2018 11:50 PM

    1 — I put this line in the being of my .profile_bash

    /».bash_profile but claims to be $0 running under $«

    I put similar lines in all my profiles.

    here is what I see when I start a terminal session.

    Yes, my .bash_profile calls config/profile.bash. The /etc/profile and /etc/bashrc are system stuff.

    2 — run the alias command to see what was aliased.

    3 — put an echo before and after your alias command.

    4— put the alias command alone «alias» after your aliasing to see if your command worked.

    Doing this will help debug your system.

    Mar 4, 2018 11:18 AM

    You are NOT running ‘bash’, so .bash_profile is not being invoked

    the -sh says you are running sh. If you were running bash it would say -bash : but it does not.

    What does the following command say:

    If the output is

    then you need to use .profile

    Or you need to change your shell to the default bash.

    It is possible that the dscl command will say /bin/bash. If that is the case, then it is possible you have your Terminal -> Preferences -> General -> Shells to open with: (X) Command (complete path): /bin/sh

    Mar 4, 2018 1:20 PM

    Dears Guys,
    First I would like every one for helping me, It’s working well now after I changed the alias to .profile as BobHarris saidto me

    Thanks for every one

    Mar 5, 2018 12:09 AM

    First I would like every one for helping me, It’s working well now after I changed the alias to .profile as BobHarris saidto me

    Some information to keep in the back of your mind.

    By default macOS sets the default shell to bash. The fact that you are using sh means there has been some change made to your macOS setup. Whether you did this intentionally, or by accident, you should keep this in mind.

    And the reason you should keep this in mind is that if you create a new account, or switch to a different Mac, you may find that in the new environment bash is now your shell, and assumptions you were making in your current environment are not working the same in the new environment.

    And now that you have BOTH a .bash_profile _AND_ a .profile, you should know that if you magically switch back to bash, that when bash starts up it looks for one of the following 3 initialization files in the following order, and will use the first it finds, then ignore all the others

    I mention this, because if you end up back with bash in your current account, it will start using the .bash_profile you currently have, and ignore the .profile you may have built up. This will be confusing, so keep this bit of information in the back of your mind. Unless of course you delete the .bash_profile, then bash will also find your .profile.

    Источник

    Читайте также:  Принципы работы реестра windows
    Оцените статью