3

On my macbook when in iterm I run

alias

I can see a bunch of aliases, huge amount are for git, and mostly they are ok, but I'd like to change some.

I tried to add few to ~/.bash_profile:

alias gcm='git commit -m'
alias c='clear'

but it had no influence on the output of the alias.

Yes, I reloaded the iterm.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • try `source ~/.bash_profile`. Now, does it work? – John_West Feb 07 '16 at 16:20
  • @John_West unfortunately not yet – Andrey Deineko Feb 07 '16 at 16:22
  • What's your alias syntax? – John_West Feb 07 '16 at 16:23
  • 1
    @John_West edited the question – Andrey Deineko Feb 07 '16 at 16:24
  • You can change them in your own files even if they already exist. It depends on the order of file execution. Start-up files are the usual place to store aliases, but the order is highly tailorable. Look at `man bash` and search for `INVOCATION`. – cdarke Feb 07 '16 at 16:24
  • @cdarke, it says `When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.` – Andrey Deineko Feb 07 '16 at 16:26
  • Maybe silly - does your `iterm` run `bash` by default? Check here: `iTerm -> Preferences -> Profiles Tab -> General` You could have different shell. – John_West Feb 07 '16 at 16:27
  • 1
    Funny, that's what my man pages say as well. One way to find how they are loaded is to run `bash -xl` (that's minus ex ell). This will give a trace of the startup files (that's the x) for a login shell (that's the ell). – cdarke Feb 07 '16 at 16:35
  • However, if `source` does not work... Wrong syntax in `~/.bash_profile` somewhere above? Simply, is there a command `exit` before your aliases? :) – John_West Feb 07 '16 at 16:35
  • @John_West actually my `.bash_profile` looks suspiciously short `[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*` – Andrey Deineko Feb 07 '16 at 16:38
  • The `-s` checks if the file exists, if so (the `&&`) then "source" the file. sourcing a file executes the commands in the current shell (otherwise it would execute it in a child process which would not affect the current one). So obviously these aliases might be in those other files, like `.profile`. I think `rvm` is the Ruby startup (?). – cdarke Feb 07 '16 at 17:10
  • Please, edit your question with your full `.bash_profile` – John_West Feb 07 '16 at 17:59
  • The question `where` is quite simple: `.bash_profile`, `.bashrc`, `.profile` and the similar in `/etc` is the common place. See [there explained](http://stackoverflow.com/a/416931). Also all scripts included (`./` or `source`) into those scripts are also executed on sessions start, your Ruby Virtual Machine `rvm` is an example. I don't understand why your custom `alias`es do not appear in `alias` list. – John_West Feb 07 '16 at 18:03
  • Oh, have you already had `gcm` and `c` aliases defined (by git, etc...)? The problem is the script running sequence then. – John_West Feb 07 '16 at 18:13
  • @John_West yea, I had `gcm` already defined, and that is my concern - I did not do so, so wanted to know, where did the come from – Andrey Deineko Feb 08 '16 at 07:10
  • No, there are more places where aliases can be stored, e.g./etc/aliases, /private/etc/aliases, ~/zshrc (if zsh used) etc – hipertracker Jun 29 '23 at 12:43

3 Answers3

2

"Where all these aliases are stored?"

~/.bashrc - this is a script invoked when you start your common terminal (iterm, etc.) because you invoke the non-login shell. The difference between login and non-login sheels is beautifully explained here: https://unix.stackexchange.com/a/46856/58326

Every script invoked from the other script can contain aliases as well.

So, I suggest you to add your aliases to the end of ~/.bashrc if you want to overwrite the aliases created in other scripts.

You can add your custom aliases wherever you want (e.g., als.sh) and load them only when you want: source als.sh

Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
  • thx a lot for a decent explanation! In the evening, when I'm at home I will test it out and get back to you, for now definitely upvote, thx again! – Andrey Deineko Feb 08 '16 at 10:43
  • Not at all! Meanwhile, I fixed the answer now :) there was an error earlier – John_West Feb 08 '16 at 10:54
  • FYI since Catalina the default shell changed from Bash to ZSH, so the equivalent file is now `~/.zshrc` – Kip Mar 17 '22 at 18:17
1

Did you log out and log in again? I think ~/.bash_profile is the correct file, so it should work.

How to reload the bash aliases was answered before:

How to reload .bash_profile from the command line?

Community
  • 1
  • 1
oli-ver
  • 362
  • 1
  • 13
0

You can search whether the alias already exists using this:

alias | grep keyword

If it exists you can remove the already existing one using

unalias alias_name

Then add your new alias. done!