0

I have some some scripts in a git project I wan to access globally, so I add the relative path to the scripts folder like so

vim ~/.bash_profile
export PATH="~/git/scripts/:$PATH"
source ~/.bash_profile

and there is a file ~/git/scripts/ called echoHelloWorld.sh, so how I can I run echoHelloWorld.sh if I am for example in the ~/Downloads folder? Do I do

cd ~/Downloads
./echoHelloWorld

because I have tried that, but it does not work, and I have also tried chmod +x echoHelloWorld.sh with no results.

1

TL;DR

Your path statement in ~/.bash_profile should look like this:

PATH=$PATH:~/git/scripts/

The basics.....

There are three problems with the way you wrote it:

  • Shouldn't use the export function since the variable (PATH) is already in the environment. When you logged in, the PATH variable was created and set. You don't need to export it as it already exists.

  • Remove the double quotes. Double quotes cause the path to be read literally so the ~ home directory expansion never takes place. For tilde expansion to work, it can't be enclosed in quotes. If you want to verify this, at the command line type the following:

    $ PATH="~/git/scripts:$PATH"
    $ echo $PATH
    ~/git/scripts:/opt/local/bin:...                  <====== INCORRECT RESULT
    
    $ PATH=~/git/scripts:$PATH
    $ echo $PATH
     /Users/foouser/git/scripts:/opt/local/bin:.....  <====== CORRECT!
    
  • Your tilde expanded (personal home directory) paths should be at the end of your path statement. Your path is read from left to right, taking precedence as it goes. In other words, if there is a command/function in your home directory with the same name as something already in your PATH it will be executed (found) first and may have unintended consequences.

Once you make the change to your ~/.bash_profile you can restart your session or just source it as you did before and the changes will take place. You can confirm that it worked by issing either (or both) the following commands:

$ echo $PATH
$ which echoHelloWorld.sh

The first will output the PATH as set and the second will tell you where in the path it found your script.

  • While I agree with you that ~/git/scripts:$PATH is incorrect, bash will perform tilde expansion within each PATH component, unlike other shells. In this case, export PATH="~/git/scripts/:$PATH" , the tilde is not a relative path. – fd0 Dec 12 '18 at 13:27
  • @fd0 - it won't expand it if you put it in quotes. – Allan Dec 12 '18 at 13:33
  • Yes, I understand that but actually the component ~/git/scripts does in bash. – fd0 Dec 12 '18 at 13:36
  • @fd0 - then I'm confused by your comment because I explain that in my bullet second point. – Allan Dec 12 '18 at 13:38
  • 1
    What @fd0 is saying is something else: In bash, ~/git/scripts is a valid component of PATH which will get searched as expected (aka the ~ gets expanded during search). See gist.github.com/nohillside/b1eb0d5b0718b77384fb0daefc04dbff – nohillside Dec 12 '18 at 16:43
1

If you have added the path of the file to run to your PATH variable you can run your command directly with

 echoHelloWorld.sh

independent of the directory you are in. Running it as ./echoHelloWorld.sh actually explicitly tells the shell to ignore PATH and run the script from the current directory (where it is not available).

0
  1. You need to make your command executable.

    chmod +x echoHelloWorld.sh 
    # verify it worked.
    ls -l echoHelloWorld.sh 
    
  2. edit your profile, which most often is ~/.bash_profile Add the directory to your path variable.

    # some people recommend putting your scripts at the end of path. 
    # Less risk of you accidentally overriding a system file name.
    PATH="$PATH:${HOME}/git/scripts/"
    
  3. I'd quit the session. You don't know if your profile script will like to be run twice.

  4. start a new session

  5. run by

    echoHelloWorld.sh
    

Here is my example:

$ oneApp
-bash: oneApp: command not found
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Developer/Tools:/Users/mac/.nexustools
$ export PATH="~/oneFile:$PATH"
$ echo $PATH
~/oneFile:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Developer/Tools:/Users/mac/.nexustools
$ oneApp
Running oneApp
$ cat oneApp
cat: oneApp: No such file or directory
$ cat ~/oneFile/oneApp 
#!/bin/bash
echo "Running oneApp"
$ 
  • 1
    Which file does the OP need to edit in step 2? – nohillside Dec 12 '18 at 8:15

You must log in to answer this question.

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