Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset / Reloading bash, completely (alias and function)

Tags:

bash

unix

exec

I want to reset the shell, as I log out / log in, reloading aliases, functions, from scratch.

But don't talk about source ~/.bashrc nor . ~/.bashrc !

Why ? Because source or . just enrich the current shell, with new function(s), alias(es), and so on.


FYI, you can put this function in your bashrc :

function foo {
  echo "foo";
}

Then do source ~/.bashrc or . ~/.bashrc. Yeah, foo is working. Then now, edityour .bashrc, and replace foo by bar, to have :

function bar {
  echo "bar";
}

You can now type foo, and saw that the function foo is still working, despite of it doesn't exist anymore in the .bashrc file. That's the point I wanted to show.


I tryed exec bash;, but it doesn'nt load the .bashrc file. And exec bash;source ~/.bashrc; obviously doesn't work, because exec kill the current process (source is never called).

like image 811
4wk_ Avatar asked Feb 17 '23 07:02

4wk_


2 Answers

as OP wish

bash --login 

NB: if you're running bash inside a terminal (xterm or alike), you need also to provide the -ls parameter (or equivalent) to the terminal. (e.g. xterm -ls )

like image 115
BigMike Avatar answered Feb 28 '23 01:02

BigMike


Write the following script:

while true; do
    bash
    if [ $? -ne 123 ]; then
        break
    fi
done

Set the executable bit and set it as your shell. Then add an alias in your ~/.bashrc:

alias resetterm="exit 123"

This requires just one extra bash process running all the time. Each time you reset, a new bash will run, and the old process will end.

like image 39
Michał Trybus Avatar answered Feb 28 '23 02:02

Michał Trybus