I have bunch of shell scripts to run codes for different parameters in sequence. For example, one of such file contains line like:

./exec30_10 55000 6234611 41 0 -4 1
./exec30_10 55000 6234611 41 0 -3 1
./exec30_10 55000 6234611 41 0 -2 1
./exec30_10 55000 6234611 41 0 -1 1
./exec30_10 55000 6234611 41 0 0 1

Where exec30_10 is the executable in the same directory as my .sh script. There are a bunch of them and I wanted to put all of them in a folder. But the directories for shell script will not be same now. But is there a way that I can run codes in different directories using shell scripts?


The script has a bug. The ./ on each line forces you to cd to the directory where these files live before it will work correctly.

Perhaps you could change it with something like

sed 's%\./%/path/to/dir/%' script.sh | sh

The sed command prints a modified version of the script to its standard output, and we feed that to another sh instance to execute it. (Maybe at first take out the pipe to sh so you can see what it's printing, and of course, modify the replacement string to point to the correct directory.)

Some sed variants allow you to use an -i option to write back the changes to the file instead of print them, if you should want to make this change permanent.

Generally, a correctly written script should run in any directory, and not hardcode any paths. Without more knowledge about your specific use case, it's hard to say. The faulty script would obviously be a lot more elegant if it used a loop, so there would only be one place to change; and maybe then you could make that a variable you could change at run time without modifying the script, by reading a configuration file or accepting a command-line parameter. Or maybe it should simply do

cd "$(dirname "$0")"

at the top, to encapsulate and shield the user from the requirement to run in a particular directory. (This would only change the working directory for the duration of the script; when it exits, the parent shell regains control, and of course remains in the directory where you started.) Another trick is to add the current directory to the front of the PATH, though this is perhaps slightly dubious in this case.

PATH="$(dirname "$0"):$PATH"

The loop could simply be

while IFS= read -r speed gain frob offset luck charm; do
    ./exec30_10 "$speed" "$gain" "$frob" "$offset" "$luck" "$charm"
done <<\____HERE
55000 6234611 41 0 -4 1
55000 6234611 41 0 -3 1
55000 6234611 41 0 -2 1
55000 6234611 41 0 -1 1
55000 6234611 41 0  0 1

which also somewhat documents what the parameters mean through the variable names (though obviously I just had to guess wildly because I have no idea). Or if your example is representative and most of these are not variable, perhaps

# Bash-only syntax
for((i=-4; i<=0; ++i)); do
    ./exec30_10 55000 6234611 41 0 "$i" 1

Here, I have assumed that you used cd "$(dirname "$0")" at the top of the script (and so the ./ before the executable's name can and should remain). (The variable $0 is the name of the script itself, with any leading path intact if the caller specified one.)


You can run a script by putting the absolute path (i.e. /home/bobe/script.sh) regardless of where in the filesystem it is. If you moved exec30_10 into /var for example, you could put /var/exec30_10 in a script. If you moved exec30_10 into a directory within the same directory as your script called execs you could run ./execs/exec30_10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.