Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm trying out cygwin environment and I have the following problem. I wrote simple script test.sh and chmod it (755) so I will be able to execute it.
It's very simple (test.sh)
Code:
#!/bin/sh
pwd
cd /usr/include/
pwd
I placed it on the root folder (/) and executed it from there:
Code:
$./test.sh
result from scrip:
Code:
/
/usr/include
As you can see everything looks fine. But when script finishes I'm still in the root folder (/) and not in the /usr/include ?!? Why? Everything looks fine in the scope of the shell script, but when it's finished I'm back where I begun.
The script kicks off a subshell. The cd is occuring in the subshell. Once your script completes it exits the subshell and returns you to your original (parent) shell.
I tried the same thing on VmWare virtual machine (Fedora Core 2) and the behaviour is the same. Not just that. Any exports I make in the shell script are not set anymore after script ends. How can I fix the problem?
It has the same action in true Linux and Unix. This is the way shells work so is not something with cygwin.
You could do something like "exec /bin/sh" at the end of the script. This WILL leave you in the subshell (actually a replacement of the subshell as that is what exec does - replaces running process with the process following exec). At that point you will be in /usr/include but may not have the environment the way you want it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.