0

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?

1
  • Again, you are using SPACES in the variable, just don't use trailing spaces! It should be ENV=VAR
    – Braiam
    Nov 12, 2013 at 12:54

2 Answers 2

1

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
6
  • 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, 2013 at 12:31
  • Exactly how I said. Nov 12, 2013 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, 2013 at 12:35
  • @Narwen See my new edits. Nov 12, 2013 at 12:39
  • Can you please revisit the question? I have just edited it.
    – Narwen
    Nov 12, 2013 at 12:53
0

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"
3
  • No dice. When you source a file, that file doesn't need to be executable. Nov 12, 2013 at 12:38
  • I have tried that providing executable permission but still i am getting the same result.
    – Narwen
    Nov 12, 2013 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, 2013 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .