0

I am trying to add folder to PATH variable (for specific user only).

so to ~/.bashrc i have added

export PATH=$PATH:/home/username/Tools/scripts/

within this folder i am having scripts and some shortcuts to other programs (for example: firefox.sh)

When i open new terminal, path variable is correct but when i try to do "sh firefox" i get "No such file".

I want to be able to execute for this user any *.sh script from any folder from terminal What am i doing wrong?

1
  • That is not how it works
    – Raffa
    Commented Jun 4, 2023 at 14:03

2 Answers 2

1

Scripts in your search path should have unique names(different from other system commands including shell builtins) i.e. avoid names such as firefox, vlc, nano, echo, cd, test, sudo ... etc.

Scripts in your search path are run by filename(including extensions like e.g. .sh if used) without specifying the shell interpreter(these are executables that take their arguments as script files with full path or script command strings but not an executable/scriptfile in your path that you call by just its filename) before it but rather include a shebang in it i.e. not like this:

sh scriptname

But rather like this:

scriptname

Scripts in your search path need to include a shebang(The first line in the script that tells what interpreter should be used to run the script) that looks like this:

#!/bin/bash

or this:

#!/bin/sh

or even this:

#!/bin/python3

and so on.

Scripts in your search path need to be made executable first like so:

chmod +x /full/path/to/scriptname

That's how they work.

0

bash and sh are different shells. I believe ~/.bashrc would not be read by sh. Try putting your additional to the path in ~/.profile which should get read by all shells.

Additionally, you wrote that you script is called firefox.sh so you would also need to reference it by sh firefox.sh - but this still might not work because the search path is only evaluate for executables, while in this case you try to load a script with sh and are not directly executing it.

Try

sh `which firefox.sh`

to first resolve the path. This would also fork if firefox.sh has the mode "x" (chmod +x firefox.sh)

2
  • 1
    An exported path with export will be available to sh(be it linked to dash or else*) and to any other installed shell if at least the containing file is sourced at least once per login.
    – Raffa
    Commented Jun 4, 2023 at 14:26
  • 1
    Yes, true - if sh is called from a bash this would work.
    – Alex
    Commented Jun 4, 2023 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .