17

let's say I have a script.

chmod +x myscript

Now to run this I go

./myscript

But is there any way I could just run it by typing

myscript

???

1
  • 1
    What OS (distro) are you using? Jul 24, 2015 at 21:12

7 Answers 7

16

I had same issue and seems no answer till now answers to the question clearly.

The solution to the question is to add export PATH=.:$PATH to your .bash_profile. This will include the current directory to the unix search path while it searches for the command. It is also wise to have yourself informed about the security risks of doing so.

1
  • 5
    I might suggest putting "." at the end, rather than the beginning, of the PATH. At least would help with name collisions. Cheers. Nov 24, 2016 at 15:39
7

add the path of the directory where your script is located to the PATH variable:

export PATH=$PATH:/path/to/directory
4

create symbolic link of script file in any /bin/ , /usr/bin etc.

ln -s /path_to_script/myscript /usr/bin/myscript 
2
export PATH=/path_to_folder_containing_executable/:$PATH

If you don't want to run that every time you open a new terminal, you could always add that line into your ~/.bashrc.

Good luck!

1

Put the script in ~/bin, a bin directory in your home directory. Then check that ~/bin is added to your PATH. It may be already (though you may have to logout and back in).

You can change your PATH to include ., but this is dangerous. It can lead to security problems. E.G.

The most obvious. If you set PATH to look in . first: then if a directory a has an executable file ls that does something bad. Then you type cd a; ls. Then something bad happens.

I can only remember a convenience case. If you set PATH to look in . last: You then make a program called test, when you run it nothing seems to happen (this is because it is running a test that has higher priority, and has no output, just an exit code). There can also be a problem too, if new software is installed, this new software can mask your program (though this one is no different to having ~/bin at end of PATH).

I have to directories in my home: ~/bin at end of PATH and ~/bin/override at beginning of PATH. ~/bin/override` is mostly empty.

2
  • The reason a program in your PATH named test doesn't run is because test is also the name of a shell builtin.
    – garyjohn
    Jul 24, 2015 at 21:33
  • @garyjohn yes you see why it can be a problem. Jul 24, 2015 at 21:36
1

Using bash, You can run a script without "." by passing the path to the script as argument to bash.

So for example:

bash -c /Users/you/myscript.sh
0

All the other answers posted before this one are correct.  (Although the ones that don't mention ~/.bashrc are incomplete in that regard.)  But, if you prefer not to use them, here's an alternative:

Type:

alias myscript="/path_to_folder_containing_executable/myscript"

or

myscript() { /path_to_folder_containing_executable/myscript "$@"; }

The second one (which is called a "shell function") can be typed

myscript() {
        /path_to_folder_containing_executable/myscript "$@"
}

if you prefer.  All of these define myscript to be a special type of command that gets checked before the shell looks at $PATH.

Like the export commands in the other answers, these affect only the current shell session.  To make the effect persistent, put the command into your ~/.bashrc.

You must log in to answer this question.

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