aditya.gupta
aditya.gupta

Reputation: 615

Changing directory within a bash script

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.

Upvotes: 1

Views: 2458

Answers (5)

Steve Robbins
Steve Robbins

Reputation: 13812

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?

Upvotes: 0

Florin Stingaciu
Florin Stingaciu

Reputation: 8275

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

cd myfolder
chmod 755 myscript
./myscript.sh

Upvotes: 0

Subho Halder
Subho Halder

Reputation: 1527

simple as hell

(cd myfolder && ./script.sh)

Hope this help :)

Upvotes: 2

Levon
Levon

Reputation: 143027

Why not simply try this:

cd /absolute_path/myfolder
./script.sh

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

Upvotes: 1

user529758
user529758

Reputation:

Try

cd myfolder
./script.sh

Hope this helps.

Upvotes: 3

Related Questions