Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question
    
Put it in $PATH. Thats /usr/bin or /usr/local/bin –  Miline Mar 29 at 11:21
1  
Or, create a shell function. –  kojiro Mar 29 at 17:00

4 Answers 4

up vote 0 down vote accepted

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>
    
share|improve this answer
    
Got it. Thanks guys, appreciate it. All really helpful info. –  CYQ00000A Mar 29 at 11:35

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.

share|improve this answer

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.

share|improve this answer

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

share|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 at 9:21
    
@Erathiel thanks for fix my mistake –  Vu Bui Mar 30 at 10:30
    
@Anthon I'm new here. Thanks for help! –  Vu Bui Mar 30 at 10:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.