0

I am leaning python and I need to set some things each time I start the terminal

#!/bin/bash
alias python=python3
export PS1='>'
echo "hello"

I want to use these initializations just for my python experimentation.

In order to save myself some effort I made a bash file which I run as "./init".

I put in the echo command just to see that I am indeed running the file. The hello message appears but the alias and export commands seem to be alive just for the during of the bash commands. When the file has completed with the echo hello, I type python -V and I get python 2 and not python 3. If I do the align manually, it works until I exit out of the terminal.

There are other initialization commands I want to use and a bash file seems the proper way to go. (There is also bashrc if that is what I should be using?) I must be missing something stupid in that it doesn't work.

8
  • 1
    you should learn about virtualenv – pLumo Mar 10 '20 at 9:51
  • I am starting to use virtual env as well. Still the basic bash command should be another way to do things. – Ilan Mar 10 '20 at 9:56
  • I added alias python=python3 and export PS1='>' to ~/.bashrc file, then run source ~/.bashrc. After that whenever I type python -V or open Python interpreter, I get python 3. – singrium Mar 10 '20 at 10:10
  • I called my file "init". Is the name important? How do I tell it that this is a bashrc and not a bash file? – Ilan Mar 10 '20 at 10:21
  • 1
    If you want the alias to be available in the current shell session, you need to source the file rather than execute it. See for example What is the difference between sourcing ('.' or 'source') and executing a file in bash? – steeldriver Mar 10 '20 at 11:43
0

If you want to initialize the terminal, all what you have to do is to edit ~/.bashrc file; .bashrc file is a like a script that is executed whenever you open a terminal.
For more information about .bashrc, check this link.
To do that:
1- Open your terminal and run gedit ~/.bashrc.
2- .bashrc will be opened in your editor, add the following lines to the end of the file:

alias python=python3
export PS1='>'

3- Save and close the editor.
4- To make those changes effective, close the terminal and open it again, or simply run source ~/.bashrc. You don't need to do that every time, it is needed just once.

After that every time you open the terminal, those lines will be executed/run and whenever you run python, you'll get Python3 run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.