1

I am right now running a bash script in which i need to change my directory and execute a script from the changed directory.

I want to change my folder to myfolder and run script.sh into that particular folder.

Any way to do that in Bash?

I have already tried

cd myfolder
./script.sh

But that does not work.

4
  • you wanted to run it through bash file? right? Commented Jun 12, 2012 at 18:31
  • 4
    What error is shown when it "does not work"? Commented Jun 12, 2012 at 18:32
  • That works for me. And everyone else ever. What about it is not working? Are you getting an error message? Commented Jun 12, 2012 at 23:00
  • I think what he means to tell is if he runs the code from one bash script to execute another bash script which is in another directory. Commented Jun 13, 2012 at 1:24

5 Answers 5

3

Try

cd myfolder
./script.sh

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

@aditya.gupta Try adding the following flags: -xv at the top of the script and post your error. Add those flags right aftr #!/bin/bash so like this #!/bin/bash -xv
Sorry if it doesn't work. You should have mentioned it in your question that you had already tried it before.
I think what he means to tell is if he runs the code from one bash script to execute another bash script which is in another directory.
2

simple as hell

(cd myfolder && ./script.sh)

Hope this help :)

4 Comments

@aditya.gupta: This is in essence the same as the one you already tried. The aspects in which it differs are not mentioned as issues in your question.
Where is script.sh located? I'm curious about how this works.
@octopusgrabbus script.sh is located inside myfolder, since in a bash script if you just write cd myfolder and in the next line write ./script.sh then it wouldn't work. I hope you get it :)
Thanks; got it. That's what I thought was happening.
1

Why not simply try this:

cd /absolute_path/myfolder
./script.sh

Specifying the absolute path makes this independent from where the script is issued.

Comments

0

Maybe its because the other script in "myfolder" does not have permisions to execute.

cd myfolder
chmod 755 myscript
./myscript.sh

6 Comments

Had already made it executable. Anyways, this was the code which made it work (cd myfolder && ./script.sh)
That doesn't make any sense. It's all the same
Nopes. Try it yourselves. echo "Initially" pwd cd /anotherfolder pwd
@aditya.gupta Did you have the line #!/bin/bash at the top of your script?
@aditya.gupta Well obviously you did something wrong. We're talking about the most basic commands of bash scripting.
|
0

Where is the script located in relation to myfolder? All these answers posted should work. For instance, if myfolder is in root, and the script is in /path/to/file.sh you would need to escape first.

cd ../../myfolder
./script.sh

You could check this too by putting an ls in your script. If you don't see myfolder then you're in the wrong spot.

Is your script executable? Try a chmod +x script.sh and run it again.

What error messages are you getting?

1 Comment

I think what he means to tell is if he runs the code from one bash script to execute another bash script which is in another directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.