Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

First script:

#!/bin/bash
pwd
source script2
pwd

Sedond script:

cd ..
pwd

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.

#!/bin/bash
while :
do
sleep 5  

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

((source /home/..../shelldata.txt) 2>&1) | \
sed -e "s/..shellRunner.sh..line../SH/" > \
/home/..../shellFeedback.txt

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

done

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
1  
It works fine for me if I do the exact same thing. –  Sebastian Paaske Tørholm Jun 8 '11 at 12:37
add comment

5 Answers 5

up vote 5 down vote accepted

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

#!/bin/bash
pwd
. ./script2
pwd

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
add comment

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
add comment

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
add comment

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

#!/bin/bash
pwd
cd ..
pwd

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
add comment

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
add comment

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.