2

I'm using bash, and I want to be able to execute a script just by typing its name as a command, same as pwd for example.

Is there a specific directory where I need to save my script to, or any other system files I need to edit to achieve this?

2
  • Put it in $PATH. Thats /usr/bin or /usr/local/bin Mar 29, 2015 at 11:21
  • 1
    Or, create a shell function.
    – kojiro
    Mar 29, 2015 at 17:00

4 Answers 4

3

You can check what locations are currently checked for direct commands by looking at the $PATH variable:

echo $PATH

It's likely this includes /usr/local/bin, in which case you could put a symbolic link there:

ln -s /opt/mysuperscript /usr/local/bin/mysuperscript

Now you can just type mysuperscript to run your script.

2

In addition to making sure the script is in the $PATH, you also must make the script executable. chmod +x SCRIPTNAME is how you do that.

1

You have to install that script in one of the directories of $PATH. Use (echo $PATH) to see the directories of $PATH

  • That means either copy the script to

  • Or make a symbolic link to the script inside one of the directories of $PATH

  • Or append the script directory to $PATH

    export PATH=$PATH:<script directory>
    
1
  • Got it. Thanks guys, appreciate it. All really helpful info.
    – CYQ00000A
    Mar 29, 2015 at 11:35
0

You may want to check the ln command. It can be used to create a link to a file or directory. Try this link to get more infomation: ln command example

1
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Anthon
    Mar 30, 2015 at 9:21

You must log in to answer this question.

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