1

I have 3 sh scripts in 3 different directories.

Instead of going into each directory and executing each script I wanted to trigger all 3 scripts from one central script (located in the superfolder above).

I tried using /bin/bash /path/to/script but this would assume the superfolder location as the location to work in.

Therefore I tried going into each folder first cd folder1 before execution but that would not work either.

I just want to trigger those 3 scripts without changing its local environment

Wandang
  • 912
  • 2
  • 8
  • 37
  • *What* exactly didn't work using `cd folder1`? There is no difference between executing that command in the parent script and changing to the folder in your interactive shell before starting the child manually, unless you have some sort of alias or wrapper around the built-in `cd` command. – chepner May 23 '17 at 13:09

3 Answers3

3

Please try the following:

( cd dir1; ./script1 )
( cd dir2; ./script2 )
( cd dir3; ./script3 )

Note that () are needed to save/restore you master script current directory.

Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
0

Using a sub-shell, when sub-shell exits the current shell is not changed

( cd path ; ./script )
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
0

The *nix way to do this is to run the scripts with their full or relative paths as you did originally. The scripts should ideally do the same thing no matter what PWD was for the original user. If you really need to know the working directory of the script, for reasons like sourcing neighboring files, just beware that it adds a bit of complexity and may not work in ancient Bash versions.

l0b0
  • 55,365
  • 30
  • 138
  • 223