Solved

How to source .bash_profile?

Posted on 2012-04-04
18
918 Views
Last Modified: 2013-11-13
Hi Experts,

I am trying to reload a environment variable that I added to my .bash_profile.  And, based upon my research I should type the command "source .bash_profile" at the $ prompt.
But, after doing so this is displayed on the next line, "=>".  Can someone tell me what is supposed typed at this point?

Thanks in advance!
0
Comment
Question by:studious01
  • 7
  • 4
  • 4
  • +1
18 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 37808130
You don't have to actually use "source .bash_profile" but can just limit Yourself to .bash_profile
0
 
LVL 21

Expert Comment

by:oleggold
ID: 37808137
Try ".bash_profile" without anything in Your script,sometime You need to  give a space between "." and bash_profile or use absolute path ,if Your script isn't in the same directory .bash_profile is.
0
 
LVL 21

Expert Comment

by:oleggold
ID: 37808153
other option use "~/.bash_profile" that by default replaces "source" by going to You home directory .bash_profile ,
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 21

Expert Comment

by:oleggold
ID: 37808154
also make sure You're using right shell,eg:
!/etc/bin/sh
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 37808573
use the blow command

. ~/.bash_profile

please note the dot and space above
0
 

Author Comment

by:studious01
ID: 37808697
I just tried typing in '. ~/.bash_profile and received the same results.
Also, I should mention the steps that are taken before typing the command above.
1. I sudo from the oracle user to user1
2. I cd  to the home directory for user1 where the .bash_profile file resides, which is /home/user1
3. Then I type the command above which is, '. ~/.bash_profile'

Output
-----------
localhost:user1:orcl:/home/user1
=>

Is there something that I am supposed to type next?
Lastly, I typed echo $SHELL and received back /bin/bash.

Thanks again!
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 37808704
what is the content of the .bash_profile?

What is your expectation from sourcing the file?
0
 

Author Comment

by:studious01
ID: 37808764
This is from the beginning of the .bash_profile:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
      . ~/.bashrc
fi

But, I just added this variable:
NOW='$date'
export NOW

And, I would like to be able to use the $NOW within the current session.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 37808770
what you get when you run

echo $NOW

or

set

or

env

also what are you expecting from

NOW='$date'
0
 

Author Comment

by:studious01
ID: 37808979
If I type echo $NOW, I get a blank line.  And, if I type env that variable does not appear.
Also, I should have type it has NOW=$date.  I would like the date to be returned, but even if I type a string as 'TEST',  a blank line is still returned.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 37809024
Hi,


You either run the command like that

. ./.bash_profile

Open in new window


please note the dot and space before ~/.bash_profile

or

source ~/.bash_profile

Open in new window


The problem with your syntax was you did not mention any path so the source command could not find your .bash_profile.

Cheers,
K.
0
 

Author Comment

by:studious01
ID: 37809104
KeremE- So, in both examples that you gave I must indicate the path?

So, the syntax would be:

. ./home/user1/.bash_profile

or

source ~/home/user1/.bash_profile

Because, I typed . ./home/user1/.bash_profile and received the following error message,
"bash: ./home/user1/.bash_profile: No such file or directory

And, I received the same error message for the second command as well.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 37809147
Nope you are doing it wrong:

./home/user1/.bash_profile

means you will be looking for home/user1/.bash_profile under the current directory Probably you're under your home. It would only find the file if you are on the root "/" directory. What you'll need to type is just: ./.bash_profile if you're already on your home directory or ~/.bash_profile if you're cd'd to anywhere else.

Don't forget that "." means the current directory and it is most likely be your home (/home/user1) and  
./home/user1/.bash_profile  means /home/user1/home7user1/.bash_profile which is unlikely to be a valid directory.

Also ~/home/user1/.bash_profile does not work since "~" already a shortcut for the users home (/home/user1) so
~/home/user1/.bash_profile means /home/user1/home/user1/.bash_profile. It does not work for the same reason...

Cheers,
K.
0
 

Author Comment

by:studious01
ID: 37809194
okay, I typed ./.bash_profile at the home directory and then echo $NOW and the result was a blank line that followed.
Also, if I type a space after the ".", while at the home directory (. /.bash_profile), then the error message of "bash: /.bash_profile: No such file or directory" is received.

Do you have any other suggestions?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 37809213
okay, I typed ./.bash_profile at the home directory and then echo $NOW and the result was a blank line that followed.

So if you did not modify the contents of your .bash_profile earlier. It won't be adding a variable called NOW so this normal..

Please use the "ls -al" command to see the files under your home directory.

Cheers,
K.
0
 

Assisted Solution

by:studious01
studious01 earned 0 total points
ID: 37809304
I ultimately placed the variable in the .bashrc file and it worked fine.

code added to .bashrc file:
export NOW=$(date)

Then, use the "." builtin to put the new value assigned to the NOW variable into effect.
. ~/.bashrc

Yes, I did some reading form my Linux book.  But, I still don't know why it didn't work from
the .bash_profile file.

I appreciate all of the responses.
0
 
LVL 30

Accepted Solution

by:
Kerem ERSOY earned 500 total points
ID: 37810171
Then, use the "." builtin to put the new value assigned to the NOW variable into effect.
. ~/.bashrc

As I've indicated earlier don't forget that "." will return the current directory in absolute path format (i.e., with leading "/") so does "~".

But in the ". ./.bashrc" usage the beginning dot means that the following command should be executed in the current shell. If it was not then the .bashrc would  run in a new shell and then all the exports (NOW variable) would be lost when the shell terminates...

Yes, I did some reading form my Linux book.  But, I still don't know why it didn't work from
the .bash_profile file.

This is because .bash_profile is executed iff it is in .bashrc. If not it will not be executed. The only profile executed during logon is the default /etc/profile and .bashrc. If you had included the .bash_profile into .bashrc it would then be executed during logon.

Cheers,
K.
0
 

Author Closing Comment

by:studious01
ID: 37991637
I also did some research of my own.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to moveā€¦
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question