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?

  • Put it in $PATH. Thats /usr/bin or /usr/local/bin – Milind Dumbare Mar 29 '15 at 11:21
  • 1
    Or, create a shell function. – kojiro Mar 29 '15 at 17:00
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>
    
| improve this answer | |
  • Got it. Thanks guys, appreciate it. All really helpful info. – CYQ00000A Mar 29 '15 at 11:35
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.

| improve this answer | |
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.

| improve this answer | |
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

| improve this answer | |
  • 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 '15 at 9:21

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.