When you type ./script.sh you are saying run from my current directory (./). Just like you have it listed ~/dir1/dir2/dir3/script.sh is one way to run it from that directory. Beware some scripts are poorly written and assume they are being run from the same directory that they are in so the script might not run correctly. Answer from ProgrammerByDay on reddit.com
Discussions

shell - How can Bash execute a command in a different directory context? - Stack Overflow
I have a common command that gets called from within very specific directories. There is only one executable sitting in /bin for this program, and the current working directory is very important for running it correctly. The script affects the files that live inside the directory it is run within. Now, I also have a custom shell ... More on stackoverflow.com
🌐 stackoverflow.com
linux - Execute a specific command in a given directory without cd'ing to it? - Unix & Linux Stack Exchange
Is there a way to execute a command in a different directory without having to cd to it? I know that I could simply cd in and cd out, but I'm just interested in the possibilities of forgoing the ex... More on unix.stackexchange.com
🌐 unix.stackexchange.com
May 26, 2011
how to execute a shell script from any directory - Unix & Linux Stack Exchange
Interactively, you can always run your scripts by typing ~/yourscript.sh on the command line, but that's very limiting, especially if you want to write more scripts that call the scripts you've already written. A more integrated and flexible approach involves putting all your scripts into one directory and adding that directory to your PATH environment variable. After you do that, you'll be able to type yourscript.sh, and no matter which directory you're in, the shell ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
linux - Execute bash script from one into another directory? - Stack Overflow
I have 3 directories: /A/B/C and 1 bash script in C directory. How can I execute this bash script from A into in C directory. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › scripting › run a script with a different working directory
Run a Script With a Different Working Directory | Baeldung on Linux
March 18, 2024 - $ cat start.sh #!/bin/bash pushd ... ~/Documents/shell/chdir Starting script Running script · In the above script, we’ve used pushd and popd commands to switch between the directories....
Top answer
1 of 11
28

sh /path/to/script will spawn a new shell and run she script independent of your current shell. The source (.) command will call all the commands in the script in the current shell. If the script happens to call exit for example, then you'll lose the current shell. Because of this it is usually safer to call scripts in a separate shell with sh or to execute them as binaries using either the full (starting with /) or relative path (./). If called as binaries, they will be executed with the specified interpreter (#!/bin/bash, for example).

As for knowing whether or not a script will find the files it needs, there is no good answer, other than looking at the script to see what it does. As an option, you could always go to the script's folder in a sub-process without leaving your current folder:

(cd /wherever/ ; sh script.sh)
2 of 11
8

You can definitely do that (with the adjustments the others mentioned like sudo sh /pathto/script.sh or ./script.sh). However, I do one of a few things to run them system wide to not worry about dirs and save me useless extra typing.

1) Symlink to /usr/bin

ln -s /home/username/Scripts/name.sh /usr/bin/name

(be sure there is no overlapping name there, because you would obviously override it.) This also lets me keep them in my development folders so I can adjust as necessary.

2) Add the Scripts dir to your path (using .bash_profile - or whatever.profile you have on your shell)

PATH=/path/to/scripts/:$PATH

3) Create Alias's in the .bash_profile in ~/.bash_profile add something like:

alias l="ls -l"

As you can tell, the syntax is just alias, digits you want to act as a command, the command. So typing "l" anywhere in the terminal would result in ls -l If you want sudo, just alias sl="sudo ls -l" to note to yourself l vs sl (as a useless example).

Either way, you can just type sudo nameofscript and be on your way. No need to mess with ./ or . or sh, etc. Just mark them as executable first :D

Find elsewhere
🌐
LinuxQuestions.org
linuxquestions.org › questions › linux-newbie-8 › running-a-bash-file-from-different-directory-4175673053
running a bash file from different directory
I have a bash script called bash.sh in the /home/operator/Desktop/Python/backup/ directory. I have added the path to the PATH [operator@localhost
🌐
Unix.com
unix.com › shell programming and scripting
How to execute script on files in another directory? - Shell Programming and Scripting - Unix Linux Community
October 20, 2017 - Hi Guys, Is there any way I can execute my bash script on files in a different folder than what the script is in? Here is an excerpt of my script: #!/usr/bin/bash input_path="/cygdrive/c/files" output_path="/cygdrive/c/files/data" #script uses files from /cygdrive/c/files directory, processes them and saves them to "/cygdrive/c/files/data" directory #processing; BODY OF SCRIPT My script is in /cygdrive/c/unix and I want execute it from there but use the files in the /cygdrive/c/files direct...
🌐
Unix.com
unix.com › shell programming and scripting
How to execute korn shell script from different directory - Shell Programming and Scripting - Unix Linux Community
October 19, 2017 - Guy’s , I need to run korn shell script from different directory, usually I run the script using ./details.ksh in the same directory but now I need to run the file and process details using awk code. Now I am running t…
🌐
CSEstack
csestack.org › home › how to run bash script from any directory [step-by-step commands]
How to Run Bash Script from Any Directory [Step-by-Step Commands]
March 28, 2021 - If you want it to make it work immediately instead of restarting the shell, run the following commands. ... If you get an error as “Permission denied”, you have to permit executable permission using commandchmod ... Now, the setup is done and you can execute shell script from any directories.
Top answer
1 of 2
6

You can call your script using only the whole path, without the dot .:

/path/to/script

sudo also works fine:

sudo /path/to/script

Will they usually find all the stuff they need to run properly?

Do you mean like, "will my script find the files which are in the same folder?" That depends of your code. For example, if you have the script /tmp/test.sh:

#!/bin/bash
ls

If you invoke it from your home folder, it will run ls in your home:

test@ubuntu:~:/tmp/test.sh
Desktop     Dropbox         Imagens  NetBeansProjects     Público     
Documentos  Modelos    R          Vídeos

In this situation, dirname is your ally:

#!/bin/bash
current=$(dirname $0)
ls $current

Running it from your home folder, it gives:

test@ubuntu:~:/tmp/test.sh
acroread_1000_1000     hiRF7yLSOD              pulse-2L9K88eMlGn7   
unity_support_test.0    clementine-art-jt5332.jpg

which is the content of my /tmp/ folder.

2 of 2
3

Common *nix practice for user scripts is to put them in your bin directory. That way, the script will be searched for by default when you type the name from the command line.

Your bin (binary) directory should be in your home folder so the full path would be /home/yourusername/bin. (Obviously change yourusername to your login name.)

The bin directory should have the following permissions if you run ls -lh from the terminal: drwxr-xr-x

By default when you make a directory with the terminal you have the following permissions set: drwxrwxr-x

The only thing you need to change is the group permission, but for I will walk you through each step from the terminal (assuming you don't already have a bin):

  • Open a terminal ([Ctrl] + [Alt] + t)

  • Create the binary directory: mkdir bin

  • Now either use the GUI or CLI to copy your scripts to /bin.

  • Now we have to change some permissions so that everything is set up correctly:

  • chmod -R 755 bin

  • This sets the owner (you) to read, write and execute, groups and others to read and execute the scripts. The -R means recursive or set the permissions for all the files in the directory as well.

For more help on file permissions see this document.

Now, you can just type myscript to execute your scripts - you do not have to be in the directory. You don't need the . / or anything!

For sudo it's a little different as you will have to define the full path to the script file.

🌐
Vultr
docs.vultr.com › how-to-execute-a-bash-script-from-any-directory
How to Execute a Bash Script from Any Directory Guide | Vultr Docs
November 6, 2025 - Move your script to the ~/.local/bin. ... Check if it is in your PATH. ... If the command returns no output, the ~/.local/bin directory is not included in your PATH. You’ll need to add it manually to your shell configuration file. Add the ~/.local/bin directory to your shell configuration file - ~/.bashrc. ... Reload your shell configuration file for the change to take effect. ... Check that the ~/.local/bin directory is now added to your PATH. ... Run your script from any directory.
Top answer
1 of 5
9

The reason that your code does not work is because of what happens when a filename globbing pattern expands.

The pattern would expand to a list of matching pathnames. The code in your script would call sh with this list. This means it would execute the first matching name as the script and give the other names as arguments to that script:

sh /var/scripts/dir/my_first.inc.sh /var/scripts/dir/my_second.inc.sh /var/scripts/dir/my_third.inc.sh

Instead, just iterate over the matching names:

#!/bin/sh

for name in /var/scripts/*/my_*.inc.sh; do
    sh "$name"
done

This assumes that each of the matching names is a script that should be executed by sh (not bash). If the individual files are executable and has a proper #!-line, then remove the sh from the invocation of the script in the loop above. If the files are "dot scripts", i.e. script that should be sourced, then replace sh by . instead to have the script execute in the current script's environment.

Note that the script above can be an sh script (#!/bin/sh) as it does not use any bash features. If the other script are "dot scripts", then you may obviously have to change this to #!/bin/bash or whatever other interpreter is needed to source the scripts.

2 of 5
5

A .inc.sh extension suggests those .inc.sh file should be included, or in other words, that their contents should be evaluated as shell code by one shell interpreter, so that that same shell interpreter can execute some other code with the functions, variables, aliases... defined in those files available.

bash's syntax is mostly backward compatible with the sh syntax, especially when its POSIX mode is enabled (bash is actually a sh interpreter when invoked as sh), so even though those functions are presumably written in sh language, you should still be able to have a bash interpreter interpret them.

One notable difference between bash (when not in POSIX mode) and sh is that bash doesn't enable alias expansion in scripts. That can be worked around though by setting the expand_aliases option or the posix option, so you can do:

#! /bin/bash -
set -o posix # increase POSIX sh compatibility, some but not all bash-specific
             # extensions are still available.

for file in /var/scripts/*/my_*.inc.sh; do
  . "$file"
done

# rest of the code that uses the functions/variables, etc defined
# in the files sourced above with the "." special builtin

Note that if there's no matching file, the . command will fail when trying to source a non-existing file called literally /var/scripts/*/my_*.inc.sh and exit the shell.

If instead in that case you wanted to not do anything you could do:

shopt -s nullglob
files=(/var/scripts/*/my_*.inc.sh)
shopt -u nullglob

for file in "${files[@]}"; do
  command . "$file"
done

With nullglob, globs with no match expand to the empty list instead of the unexpanded pattern. With the command prefix, failure of special builtins like . (like when $file is not readable) doesn't cause the shell to exit (and error should still be reported).

To look for .inc.sh files recursively instead of just the subdirs of /var/scripts, you could use the globstar option to enable the ** operator:

shopt -s nullglob globstar
files=(/var/scripts/**/my_*.inc.sh)
shopt -u nullglob globstar

In any case, directory and files whose name starts with . (hidden ones) are omitted. Use the dotglob option if you want to include the,

Obviously, if any of the sourced scripts calls exit or exec or has a syntax error or any of the special builtins called within fail, that will also exit the script.

Top answer
1 of 3
1

First confirm that the configure script is present where you think it is. Second make sure that it's executable:

$ ls -l ./configure 
-rwxrwxr-x 1 saml saml 100 Jun  9 05:11 ./configure

If both of the checks are OK then you might want to try running configure by first changing directories to /mainfolder/execution and then running configure like this:

$ cd /mainfolder/execution
$ ../configure

You also might want to try it this way:

$ cd /mainfolder
$ ./configure execution

EDIT #1

According to comments left by OP, the following directory structure appears to be what he's describing:

$ tree -f
.
`-- /mainfolder
    `-- /mainfolder/execution
        `-- /mainfolder/execution/configure
2 of 3
0

In *nix / always is the root directory of the whole file system and . always refers to the current working directory.

Every path starting with / is an absolute path. Every path starting with . is a relative path.

Hence /home/... is probably correct, as this directory lives it the root directory, whereas /excecution is probably wrong as this directory does not live in the root directory, but somewhere under /home/....

If you say ./configure, you try to run the file configure living in the current working directory (.).

The error No such file or directory, well, just says that there is no file named configure in the current working directory.

You can use pwd to show, what the current working directory is. Use ls to show what files exist there. Use cd to change the current working directory.

To help you with your actual problem. You need to say what you were trying to do. Why do you want to run configure? What is it supposed to do? Why do you think you need to specify the execution directory?