as @Baenyth said: what you are trying to do is impossible. or better: how you are trying to do it is impossible.
first: your bash command is wrong. bash -s command
does not execute command
. it just stores the string "command" in the variable $1
and then drops you to the prompt. that is why the python script seems to freeze. what you meant to do is bash -c command
.
second: why do you source .bashrc
? would it not be enough to just source .bash_aliases
?
third: even if you got your bash command right, the changes will only take effect in that very session. once that bash session is closed, and your python script returns, you are back at your first bash session, which was merilly running along in the background and did not take note of any changes.
everytime you want to change something in the current bash session, you have to do it from inside the current bash session. most of the commands you run from bash (system commands, python scripts, even bash scripts) will spawn another process, and everything you do in that other process will not affect your first bash session.
source
is a bash builtin which allows you to execute commands inside the currently running bash session, instead of spawning another process and running the commands there. defining a bash function is another way to execute commands inside the currently running bash session.
see http://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788 for more information about sourcing and executing.
what you can do to achieve what you want
modify your python script to just do the changes necessary to .bash_aliases
.
prepare a bash script to run your python script and then source .bash_aliases
.
#i am a bash script, but you have to source me, do not execute me.
python_script_to_modify_bash_aliases "$@"
source ~/.bash_aliases
add an alias to your .bashrc
to source that script
alias add_alias='source call_python_script_then_source_bash_aliases'
now when you type add_alias some_alias
in your bash prompt it will be replaced with source call_python_script_then_source_bash_aliases some_alias
and then executed. since source is a bash builtin, the commands inside the script will be executed inside the currently running bash session. the python script will still run in another process, but the subsequent source command will run inside your currently running bash session.
another way
modify your python script to just do the changes necessary to .bash_aliases
.
prepare a bash function to run your python script and then source .bash_aliases
.
add_alias() {
python_script_to_modify_bash_aliases "$@"
source ~/.bash_aliases
}
now you can call the function like this: add_alias some_alias