I have a shell script which I want to run without using the "sh" or "bash" commands. For example:
Instead of: sh script.sh
I want to use: script.sh
How can I do this?
P.S. (i) I don't use shell script much and I tried reading about aliases, but I did not understand how to use them.
(ii) I also read about linking the script with another file in the PATH variables. I am using my university server and I don't have permissions to create a file in those locations.
Add a "shebang" at the top of your file:
And make your file executable (
chmod +x script.sh
).Finally, modify your path to add the directory where your script is located:
(typically, you want
$HOME/bin
for storing your own scripts)Add . (current directory) to your PATH variable.
You can do this by editing your .profile file.
put following line in your .profile file
PATH=$PATH:.
Just make sure to add Shebang (
#!/bin/bash
) line at the starting of your script and make the script executable(usingchmod +x <File Name>
).You have to enable the executable bit for the program.
Then you can use
./script.sh
You can add the folder to the PATH in your
.bashrc
file (located in your home directory). Add this line to the end of the file:In this example the file will be called
myShell
First of all we will need to make this file we can just start off by typing the following:
Notice we didn't put the
.sh
extension? That's because when we run it from the terminal we will only need to typemyShell
in order to run our command!Now, in nano the top line MUST be
#!/bin/bash
then you may leave a new line before continuing.For demonstration I will add a basic
Hello World!
responseSo, I type the following:
After that my example should look like this:
Now save the file and then run this command:
Now we have made the file executable we can move it to
/usr/bin/
by using the following command:Just to make sure that the machine can execute it properly we will need to reboot the machine
I used
sudo shutdown -r now
Congrats! Our command is now done! In the terminal we can type
myShell
and it should sayHello World!
Just make sure it is executable, using
chmod +x
. By default, the current directory is not on your PATH, so you will need to execute it as./script.sh
- or otherwise reference it by a qualified path. Alternatively, if you truly need justscript.sh
, you would need to add it to your PATH. (You may not have access to modify the system path, but you can almost certainly modify the PATH of your own current environment.) This also assumes that your script starts with something like#!/bin/sh
.You could also still use an alias, which is not really related to shell scripting but just the shell, and is simple as:
Which would allow you to use just simply
script.sh
(literally - this won't work for any other*.sh
file) instead ofsh script.sh
.You can type sudo install (name of script) /usr/local/bin/(what you want to type to execute said script)
ex:
sudo install quickcommit.sh /usr/local/bin/quickcommit
enter passwordnow can run without .sh and in any directory