Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to change working directory of my script by another script that has been included.

First script:

source script2

Sedond script:

cd ..

The result is following:

> ./script1
/home/sebi/testdir1/testdir2  # 1st script
/home/sebi/testdir1           # 2nd script
/home/sebi/testdir1           # 1st script

I have edited the question as I tested this script and it worked as I wanted however following script works differently, as subscript finishes execution parent is in same directory as it was before invocation of the subscript.

while :
sleep 5  

cat /home/.../shelldata.txt >> /home/.../gmodshell #logging

((source /home/..../shelldata.txt) 2>&1) | \
sed -e "s/" > \

echo > /home/..../shelldata.txt


Whole aim is to access bash using files so I can write command to one file and after a while I get output in other file.

share|improve this question
It works fine for me if I do the exact same thing. – Sebastian Paaske Tørholm Jun 8 '11 at 12:37
up vote 5 down vote accepted

Assuming scripts 1 and 2 are in the same directory, try making script 1:

. ./script2

Placing the dot before the call to the script (I'm talking about the first dot, not the dot that is part of the filepath to the script you want to call) tells the shell "Execute the following script in this process rather than spawning a new process for it."

Actions taken within the second script (like cd-ing or setting variables) will persist after the second script ends.

share|improve this answer

From bash documentation:

Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation. Builtin commands that are invoked as part of a pipeline are also executed in a subshell environment. Changes made to the subshell environment cannot affect the shell's execution environment.

So you should avoid putting parentheses while sourcing a file with the . (dot) command (as mentioned in this answer).

share|improve this answer

This is not specific to sourcing a script. You would get exactly the same result with a single script. Try:

cd ..

The reason for that is that a launched script is executed in a subshell. When the script returns, the subshell is closed and you get back to your original shell, wherever you were in the script.

If you want a command to drop you in another directory, you should use an alias or a bash function instead:

function script1 () { cd ..; }
share|improve this answer
second script is dynamic, it recives commands from remote source. Is there a way instead of source to include other file as it would be ran from same file, kind of inline include? – Sebi Jun 8 '11 at 12:48
@Sebi: can you clarify your need/architecture? – ℝaphink Jun 8 '11 at 12:49
I have tested exact same script as I initially posted in the question and it worked as I wanted it however script that I added in does not work correctly. (as I wanted it to do) I am on Fedora 14. What I want to achieve is to execute remote commands (written to file) and give output in another file so that I can access bash using file functions, I know how inefficent it is but only way I can communicate is to use file functions. – Sebi Jun 8 '11 at 12:59

There is no point in using the source command if you do it in a subshell like in your second script:

((source /home/..../shelldata.txt) 2>&1) | \

Moreover, shelldata.txt is really a shell script so giving it a .txt extension, while not forbidden, is confusing.

Finally, it would help if you tell what shelldata.txt contains to figure out how to answer to your question.

share|improve this answer

Could you use the sourced script to set a variable and have the original variable make the change based on the content of the variable rather than making the change directly in the sourced script?

share|improve this answer
But the variable wouldn't exist after the second script terminated, meaning it needs to run in the current process, using the dot command. And if you're going to do it that way (run it in the current process), you may as well just do the cd, rather than set a variable and then cd into it. – Cam Jackson Jun 8 '11 at 23:24

You must log in to answer this question.

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