How Do I Make bashrc File Update Upon Changes?

Question:
I edited my bashrc file to have custom aliases, but the only way I can figure out that actually updates it is by running kill 1, and I don’t want to reload the repl every single time. Any ideas?

1 Like

With the pkgs.inotify-tools system dependency added:
If the file changes have been saved, run:

. .config/bashrc

Otherwise, you can wait for the file to change before restarting by pasting this command in Shell (or putting it in a function which you can then run):

inotifywait -qqe create .config --include=bashrc
. .config/bashrc

I’m trying to do this, but it is not working. here’s the related code:

def add_command(command, code):
    if not(os.path.exists(".congig/bashrc")):
        with open('.config/bashrc', 'w') as f:
            f.write('')
        bashrc_content = '''
    
    
        # Courtesy of https://replit-extension-shell-commands-lib.luisafk.repl.co
        # Do not edit this code!
    
        _RUN_SHELL_COMMAND_LIB_PIPE="/home/runner/$REPL_SLUG/.config/._runShellCommandLibPipe";
        _RUN_SHELL_COMMAND_LIB_OUT_PIPE="/home/runner/$REPL_SLUG/.config/._runShellCommandLibOutPipe";
        touch "$_RUN_SHELL_COMMAND_LIB_PIPE";
        touch "$_RUN_SHELL_COMMAND_LIB_OUT_PIPE";
        while true;
          do sleep 0.1;
          if [ -s "$_RUN_SHELL_COMMAND_LIB_PIPE" ];
            then 
            echo -n "" > "$_RUN_SHELL_COMMAND_LIB_OUT_PIPE";
            bash -s < "$_RUN_SHELL_COMMAND_LIB_PIPE" &> "$_RUN_SHELL_COMMAND_LIB_OUT_PIPE";
            echo -n "" > "$_RUN_SHELL_COMMAND_LIB_PIPE";
            fi;
        done &


'''
        with open('.config/bashrc', 'w') as f:
            f.write(bashrc_content)
    
    with open(".config/bashrc", "a") as f:
        f.write(f'alias {command}="{code}"\n')

    os.system("inotifywait -qqe create .config --include=bashrc")
    os.system(". .config/bashrc")

add_command(
    "hello",
    "echo 'Hello, world!'")

It doesn’t properly update the shell, so if I were to now run hello in the shell, it wouldn’t do anything.

oh, if you want to refresh the Shell after the bashrc has been updated programmatically, you’d have to use a command like

python main.py
. .config/bashrc

if you really want to make the script work on its own, you’d have to restart the whole shell process (and so also reload the nix environment):

os.kill(os.getppid(), 1)

I don’t think you can issue commands to the parent shell as you have tried to do.

It never runs any code after that (the Flask project).


Restarts so can’t use.