How to run a shell script inside a shell script

./myshell.sh means the script myshell.sh is found in the current directory. If you run this script from somewhere else, it won't work. You could use full paths, but in this case, the only sensible solutions are:

  1. Adding the location of myshell.sh to your $PATH (in case, myshell.sh really is something that is supposed to be called from everywhere). So, add PATH="$PATH":/dir/of/myshell at the beginning of the outer script.

  2. Put myshell.sh somewhere so that it's accessible from everywhere (just like all other executables on the system). That would be /usr/local/bin most likely. Use this only if this script is universally useful.

If the scripts rely on local files in their directory (and may even break down and do damage if called from elsewhere), then you should either leave it in the current form (this actually prevents you to call them from places you are not supposed to), or use cd inside the script to get to the proper location. Be careful, use absolute paths in shell scripts for cd, it is too easy to break stuff if something goes wrong and you go out .. and cd-ing further in fails, you could escape out of your directory and reign chaos all over the parent directories.

Mostly I'd recommend solution #1.


As suggested by YoMismo you should use full path in your script or cd to your scripts directory.

# content of entrance.sh
/full/path/myshell.sh arg1 arg2
/full/path2/myshell.sh arg3 arg4

Or (but this is a very ugly solution)

# content of entrance.sh
cd /full/path
./myshell.sh arg1 arg2
cd /full/path2
./myshell.sh arg3 arg4

Tags:

Bash