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.

KuboMD
  • 141
  • 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 '19 at 16:23
  • @dessert Hi Dessert, thanks for the comment. Unfortunately that Q doesn't help as I'm not trying to read out of my bashrc. – KuboMD Jul 30 '19 at 16:27
  • 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 '19 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 '19 at 17:45
  • I see the point about it being a subshell. The answers on that question aren't helping me though, I want to inject an alias and install the new .bashrc from inside my script. How can I do that? – KuboMD Jul 30 '19 at 17:56
  • 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 '19 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 '19 at 18:14
  • Can't you add the alias to .bashrc as planned and also define it back to your parent shell at the same time? – WinEunuuchs2Unix Jul 31 '19 at 01:21
  • @WinEunuuchs2Unix Not sure what you mean by define it back to the parent shell. Could you elaborate? – KuboMD Jul 31 '19 at 13:21

0 Answers0