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
???
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityI 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.
add the path of the directory where your script is located to the PATH variable:
export PATH=$PATH:/path/to/directory
create symbolic link of script file in any /bin/ , /usr/bin etc.
ln -s /path_to_script/myscript /usr/bin/myscript
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!
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.
test
doesn't run is because test
is also the name of a shell builtin.
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
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
.