2

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.

1

5 Answers 5

5

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.

2

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).

1

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 ..; }
3
  • 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, 2011 at 12:48
  • @Sebi: can you clarify your need/architecture?
    – raphink
    Jun 8, 2011 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, 2011 at 12:59
1

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.

0

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?

1
  • 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. Jun 8, 2011 at 23:24

You must log in to answer this question.

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