0

I would like to know how to run a file in a case like this example:

I have two files in the paths:

/home/example/folder1/filewithname.sh
/home/example/folder2/filewithname.sh

I have both folders added to PATH, how can I specify which filewithname.sh I'm talking about?

Thanks in advance!

3

The directories listed in the PATH variable will be searched in the order in which they are listed. When the given command is found, the search ends.

This means that if you added the two directories in the order that you show (folder1 before folder2), then the command from folder1 will be used if you just say filewithname.sh on the command line. The folder1 variant of the command effectively shadows the variant in folder2.

If you want the other variant of command, you will have to access it using its path, i.e. as /home/example/folder2/filewithname.sh, or via some equivalent pathname.

You may also create aliases for each of the commands so that you don't have to think about what order the directories are listed in $PATH. For example,

alias c1=/home/example/folder1/filewithname.sh
alias c2=/home/example/folder2/filewithname.sh

This would allow you to use c1 and c2 to execute the two scripts.

You could put these alias definitions in your shell's initialization file (e.g. in ~/.bashrc if you're using bash, or in ${ZDOTDIR:-$HOME}/.zshrc if you're using zsh).

1
  • Thanks! I ended up using a variable instead of PATH, but this is certainly useful information :) Feb 12 at 11:58
1

You can run the files individually based on their full path:

$ /home/example/folder1/filewithname.sh
$ /home/example/folder2/filewithname.sh

Or you can change to the directory of the individual files, and run the files based on their relative path. For example:

cd /home/example/folder1
~/example/folder1$ ./filewithname.sh

cd /home/example/folder2
~/example/folder2$ ./filewithname.sh

To run the files in this way, the execute bit has to be set on the files.

1
  • They seem like good options too! :) I actually kind of used this since I ended up using a variable, which at the end is like using the common full path + individual additions. Feb 12 at 12:01

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.