-1

I want to make an alias python="python3". I do this by using nano ~/.bash_profile and then typing: alias python="python3" , and save. Then I write: source ~/.bash_profile to overwrite changes. But this alias only lasts for the terminal session that i am in. When i start a new terminal session I have to write source ~/.bash_profile for the alias python="python3" in order for it to go into effect.

3 Answers3

1

Do you have in your ~/.bashrc file a command like

source ~/.bash_profile

or

. ~/.bash_profile

?

If not, you have to add it

Anyway, it's common practice to create ~/.bash_aliases to use aliases and then source it in ~/.bashrc like:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
Beglex
  • 58
  • 3
  • Hi! When I did ls -a in my home directory there was no .bashrc file there. So I did nano ~/.bashrc and in then wrote . ~/.bash_profile. But it still won't load the alias. I am quite new to the command line so what does the first part of the expression -f after the if statement mean? I understand ~/.bash_aliases refers to the file .bash_aliases in the home directory but what does '-f' mean? Is it necessary to have brackets around the expression after the if statement? What would happen if you didn't have brackets around the expression? – Karl Karlsson Aug 06 '20 at 12:40
  • If it still doesn't load alias, you need to tell more info about how you're logging in shell and what your files are containing – Beglex Aug 06 '20 at 17:06
  • Also I think it would be useful for you to know how shell files are loaded in different environments: https://zwischenzugs.files.wordpress.com/2018/01/shell-startup-actual.png – Beglex Aug 06 '20 at 17:07
  • *-f some_file* means than *if some_file is a regular file type*. Not directory, socket or any other, just regular file. Read about it here: https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html – Beglex Aug 06 '20 at 17:10
  • And the brackets - it's a *test* command. But for convenience it's bade as square brackets and you can even find it in */usr/bin/[*. And test plays test role) Read about test here: https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins – Beglex Aug 06 '20 at 17:17
0

~/.bash_profile is only sourced by interactive login shells, as the bash manpage will tell you. If you want to have it available also in interactive non-login shells, I suggest that you place the alias definitions into a separate file (say: ~/.bash_interactive) and source this file from both .bash_profile and .bashrc.

user1934428
  • 19,864
  • 7
  • 42
  • 87
-1

To make aliases permanent, you've to set them in a file that’s read when you launch the terminal. i.e. try adding your line alias python=python3 into ~/.bashrc or into ~/.profile or ~/.bash_profile for remote logins. If you want the command being executed for all users, put it into /etc/bash.bashrc. In general, aliases can be kept in the ~/.bash_aliases file and that file is loaded by ~/.bashrc. Remember to uncomment the following lines in ~/.bashrc to enable the use of ~/.bash_aliases if you're running on older versions of Ubuntu. On Ubuntu 11.04 and later, it's already enabled:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

The aliased command will be available on any new terminal. To have the aliased command on any existing terminal one need to source ~/.bashrc from that terminal as

source ~/.bashrc
1218985
  • 7,531
  • 2
  • 25
  • 31
  • It's not good practice to touch system-wide `/etc/bash.bashrc`. – sexpect - Expect for Shells Aug 06 '20 at 01:26
  • @SarathKumarSivan : From what do you conclude that a `/etc/bash.bashrc` would have an effect? This file is not mentioned in the [bash man page](https://linux.die.net/man/1/bash). – user1934428 Aug 06 '20 at 06:55
  • 1
    @user1934428 When bash initializes a non-login interactive bash shell on a Debian/Ubuntu-like system, the shell first reads /etc/bash.bashrc and then reads ~/.bashrc. The reason that /etc/bash.bashrc does not appear in normal bash documentation is that it is a feature added by Debian and adopted by Ubuntu. You can check out the Debian explanation here: https://sources.debian.org/src/bash/4.1-3+deb6u2/debian/README/#L50-L56. – 1218985 Aug 07 '20 at 00:03
  • Thank you for the explanation. It's a bit creepy if such features depend on the way bash is compiled. But we don't know whether the OP is using Debian or Ubuntu, so I think you should at least put it into your answer, that bash **may** process this file, if it is configured to do so. I wonder whether, for a given bash, we can easily find out (without much experimentation) with what flags it has been compiled.... – user1934428 Aug 07 '20 at 06:00