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 have a file name .test with a echo statement. Now I want to run this file from .bashrc file. What I have done so far is:

My .test file contains:

echo "hello everyone"

My .bashrc file contains:

echo "hello world"

Now what I want is to run .test file from .bashrc file and here is what I did

In .bashrc file

echo "hello world"
ENV=$(/home/narwen/.test)
echo $ENV

Now when I try to run the .bashrc file from prompt like this

. .bashrc

I get

hello world

and errors like this

XAUTHORITY=/home/niraj/.Xauthority
_=/usr/bin/env
=

Can somebody help me please?

share|improve this question
    
Again, you are using SPACES in the variable, just don't use trailing spaces! It should be ENV=VAR –  Braiam Nov 12 '13 at 12:54
    
Thanks man for the support. –  Narwen Nov 12 '13 at 13:04

2 Answers 2

up vote 1 down vote accepted

Just use:

. /path/to/.test

or:

source /path/to/.test

Or, if you want to keep the output of . /path/to/.test in a variable ENV, then use:

ENV=$(. /path/to/.test)

So, please don't use spaces around the = in assignments.

And later, you can use $ENV variable something like:

echo $ENV
share|improve this answer
    
Thanks for the reply but you are not getting my point. I have placed one echo statement in .bashrc file and one echo statement in .test file. Now I want to display the echo statement of .test file from .bashrc file.So how can I do it? –  Narwen Nov 12 '13 at 12:31
    
Exactly how I said. –  Radu Rădeanu Nov 12 '13 at 12:33
    
what do you mean by that? I think you are not getting me or i am unable to explain it to you. –  Narwen Nov 12 '13 at 12:35
    
@Narwen See my new edits. –  Radu Rădeanu Nov 12 '13 at 12:39
    
Can you please revisit the question? I have just edited it. –  Narwen Nov 12 '13 at 12:53

Your .test file is being executed as a script. Instead, you may need to fork a shell to apply the ENV variable to the shell:

$ . .bash
$ echo $ENV
something
$

My .test file has echo "something" as content and .bash has ENV=$(. /home/braiam/.test):

$ cat .bash
ENV=$(. /home/braiam/.test)
$ cat .test
echo "something"
share|improve this answer
    
No dice. When you source a file, that file doesn't need to be executable. –  Radu Rădeanu Nov 12 '13 at 12:38
    
I have tried that providing executable permission but still i am getting the same result. –  Narwen Nov 12 '13 at 12:40
    
@Narwen use the later part of Radu's answer, you need to fork a shell before you can source it. As you are doing it is just executing the . and .test. –  Braiam Nov 12 '13 at 12:42

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.