4

I am trying to create a script to be reload bashrc once but it did not work.

reloader.sh

#!bin/bash
source ~/.bashrc
rm reloader.sh
Peyker
  • 71
  • 1
  • 1
  • 8
  • 1
    You need to run the script with `source`. Otherwise it runs in a subshell, and the changes that `.bashrc` makes won't affect your original shell. – Barmar Mar 25 '17 at 07:04
  • If you want to make a script to load .bashrc into your current shell (the one in your terminal) just source from command line, it it is the script that needs the .bashrc then your code should actually work. Technically the answer with the tick is wrong (whatever you wanted it's a stupid way to go about it) while the one telling you to change the shebang is correct. You should pose your question more clearly. – louigi600 Dec 03 '20 at 06:17

2 Answers2

10

I had the same problem. The issue is that only interactive shells can access whatever you have defined in your .bashrc(Aliases and so on)

To make your shell-script interactive use a shebang with parameter:

#!/bin/bash  -i
Qohelet
  • 1,459
  • 4
  • 24
  • 41
7

You need to use source to run the script:

source reloader.sh

If you just run it as a command, it will run in a new process, so none of the changes that .bashrc makes will affect your original shell process.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks a lot. It worked in executing source ~/.bashrc command but it seems that the code is ignoring "rm reloader.sh" and it keeps reloading the bashrc.. Any idea how to solve this?? I am executing this script from bashrc as a way of reloading bashrc once. – Peyker Mar 25 '17 at 08:32
  • Why do you need to reload bashrc when you're already loading bashrc? – Barmar Mar 25 '17 at 08:37
  • Is it getting an error when it tries to `rm reloader.sh`? – Barmar Mar 25 '17 at 08:39
  • I am trying to open a python server script in bashrc but it gave me a binding error. However If I close the terminal and reopen it again, the server starts working properly. So I thought If I can do something to refresh the bashrc, the socket can work properly. – Peyker Mar 25 '17 at 11:46
  • Are you sure it's getting there? If you put `echo removing reloader.sh` before it, do you see the message? – Barmar Mar 25 '17 at 11:47
  • Put `set -x` at the beginning of `.bashrc`, so you can see all the commands that are executing. – Barmar Mar 25 '17 at 11:47
  • It did not execute it. I think the problem that when he executes source ~/.bashrc, it enters in a loop and keep reloading the bahrc – Peyker Mar 25 '17 at 12:11
  • There's no reason for `.bashrc` to reload `.bashrc`. What do you see when you use `set -x`? – Barmar Mar 25 '17 at 12:15
  • with 'source ~/.bashrc' the execution was fast so I could not see any clear things. But without it I can see each command executed including 'rm reloader.sh' and the script was removed. – Peyker Mar 25 '17 at 12:20
  • So I think my approach won't do since it will be resulting in running on a loop. Thanks a lot for your help. – Peyker Mar 25 '17 at 16:21