0

I have an "installer" script that installs a couple scripts and a directory for them. This works as expected. In the middle of my script I also have an alias I inject into the local .bashrc so that the script can be called from the commandline:

echo -e "..........Adding Alias"
echo "alias Change=~/ChangeTool/Change" >> .bashrc

echo -e "..............Recompiling environment"
. ~/.bashrc

I can confirm it does show up in my .bashrc when I run the installer. However, if I rm -rf my script directory, remove the line from (and recompile with . ./.bashrc) my .bashrc, and reinstall, I get "Change command not found".

I then thought to myself "o.k., I'll try again." so I remove the line and recompile bashrc myself. I more .bashrc and it's no longer there. Out of curiosity I tried the Change command and it worked.

Is there just some weird delay with installing aliases?

Edit: Here is the output on fresh install:

PSTSAUTO@MUXALRM:~$ ./installer.sh 
Installing [ ASR9K CHANGE TOOL ]
..........Targeting /home/PSTSAUTO/
..........Getting raw tarball
..............Extracting the tarball
..........Adding Alias
..............Recompiling environment
..........Install Complete.

PSTSAUTO@MUXALRM:~$ Change
Change: command not found
PSTSAUTO@MUXALRM:~$ grep "Change" .bashrc
alias Change=~/ChangeTool/Change
PSTSAUTO@MUXALRM:~$ 

So it's there but it doesn't work. As stated in code snippet above I did try to source with the . command.

9
  • 2
    What do you mean by saying “recompile .bashrc”, do you mean sourcing it with . ~/.bashrc or source ~/.bashrc? Please add new information directly to your question (edit), do not use comments.
    – dessert
    Jul 30, 2019 at 16:23
  • 1
    But you are trying to source the .bashrc file in your script for the terminal which runs the script, aren’t you? This is exactly what OP there was trying to do as well, and the answer explains nicely why that can’t work.
    – dessert
    Jul 30, 2019 at 17:39
  • 2
    You source .bashrc only in the environment of your script, not outside. It's the same issue as why (export var=bla); echo $var prints nothing. The duplicate is correct.
    – pLumo
    Jul 30, 2019 at 17:45
  • 1
    You can’t. :) There is just no way to source the changed .bashrc in the parent shell from within the subshell, the best you can do is open a new terminal tab/window with your script, but with all the different terminal emulators that’s a non-trivial task.
    – dessert
    Jul 30, 2019 at 18:03
  • 2
    @dessert Darn it. I guess that makes sense. Thanks for your help. I'm a newbie so i'll leave it to your judgement but if you supply this in the form of an answer i'd checkmark it.
    – KuboMD
    Jul 30, 2019 at 18:14

0

Browse other questions tagged or ask your own question.