Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am unable to source my ~/.profile using a bash script.

I tried:

source ~/.profile

By the way this is the script I am using

#!/bin/bash

echo Enter the shortcut, or alias, you want to use:
read SHORTTEXT
echo Now enter what text you want it to replace:
read LONGTEXT
echo "alias $SHORTTEXT='$LONGTEXT'" >> ~/.profile
echo "alias $SHORTTEXT='$LONGTEXT' was added to your profile.The alias will work after logoff/on"
share|improve this question
    
Why do you want to source ~/.profile from a script? That's seldom, if ever, any point in doing. –  geirha Jul 22 '12 at 12:05
    
I have a script that puts a new alias into my ~/.profile from command line. I have to source it subsequently. It would be really convenient if the script itself can source it. –  Chirag Jul 22 '12 at 12:25
    
First off, ~/.profile is the wrong place to put aliases, they should be in ~/.bashrc. Secondly, aliases don't work in scripts; you have to specifically enable aliases in scripts. Unless you source your script from an interactive shell, those alias settings will have no effect. –  geirha Jul 22 '12 at 12:27
    
Can you please elaborate why ~/.profile is a wrong place to put aliases. I read it somewhere on internet and it really works for me. Is there any specific diadvantage of doing so? –  Chirag Jul 22 '12 at 12:33
    
~/.profile only gets sourced by interactive login shells, not interactive non-login shells. See mywiki.wooledge.org/DotFiles –  geirha Jul 22 '12 at 12:35

2 Answers 2

up vote 3 down vote accepted

That script itself must be sourced for it to make changes to the current shell. If you run the script, a new instance of bash is started to interpret the commands in the script. This new instance cannot alter its parent, thus any aliases that are set in the script, dies with the script.

share|improve this answer
    
So what to do ideally? –  Chirag Jul 22 '12 at 12:45
1  
@ChiragVora Either have the user source the script instead of running it, or make it a function (which must be put in ~/.bashrc first). –  geirha Jul 22 '12 at 12:46
    
Thanks for your help. I did exact what you described. It works fine. –  Chirag Jul 22 '12 at 14:42

I'm way too late for this party but actually I was having exactly the same problem today. I came by to find a solution but what I found kept me thinking "this can't be the workaround to this".

So I went again to Terminator's options and the way I solved it was just by clicking a check-mark control inside the Terminator preferences. Just by checking the "Run command as a login shell" and then restarting Terminator, I was able to run "rails console" or "rails server" without having to source no file.

screenshot for the Terminator's preference window :

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.