How do I run a shell script without using "sh" or "bash" commands?
Solution 1
Add a "shebang" at the top of your file:
#!/bin/bash
And make your file executable (chmod +x script.sh
).
Finally, modify your path to add the directory where your script is located:
export PATH=$PATH:/appropriate/directory
(typically, you want $HOME/bin
for storing your own scripts)
Solution 2
These are the prerequisites of directly using the script name:
- Add the shebang line (
#!/bin/bash
) at the very top. - Use
chmod u+x scriptname
to make the script executable (wherescriptname
is the name of your script). - Place the script under
/usr/local/bin
folder.- Note: I suggest placing it under
/usr/local/bin
because most likely that path will be already added to yourPATH
variable.
- Note: I suggest placing it under
- Run the script using just its name,
scriptname
.
If you don't have access to /usr/local/bin
then do the following:
Create a folder in your home directory and call it
bin
.Do
ls -lA
on your home directory, to identify the start-up script your shell is using. It should be either.profile
or.bashrc
.Once you have identified the start up script, add the following line:
PATH="$PATH:$HOME/bin"
Once added, source your start-up script or log out and log back in.
To source, put
.
followed by a space and then your start-up script name, e.g.. .profile
or. .bashrc
Run the script using just its name,
scriptname
.
Solution 3
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 just script.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:
alias script.sh='sh script.sh'
Which would allow you to use just simply script.sh
(literally - this won't work for any other *.sh
file) instead of sh script.sh
.
Solution 4
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:
sudo nano myShell
Notice we didn't put the .sh
extension?
That's because when we run it from the terminal we will only need to type myShell
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!
response
So, I type the following:
echo Hello World!
After that my example should look like this:
#!/bin/bash
echo Hello World!
Now save the file and then run this command:
chmod +x myShell
Now we have made the file executable we can move it to /usr/bin/
by using the following command:
sudo cp myShell /usr/bin/
Congrats! Our command is now done! In the terminal we can type myShell
and it should say Hello World!
Solution 5
You have to enable the executable bit for the program.
chmod +x script.sh
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:
export PATH=$PATH:/your/folder/here
Related videos on Youtube
Rameez Hussain
Updated on September 09, 2021Comments
-
Rameez Hussain over 2 years
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.
-
Jonathan Leffler over 12 yearsThe answers indicate how to make the script executable, but don't seem to cover how to make it accessible. For that, create yourself a bin directory -
$HOME/bin
- if you don't already have one, add it to your PATH (near the front). Put the scripts (and any other programs) you want to execute directly without specify the pathname in here, or symlinks in here to where the actual programs are. -
PJ Brunet over 7 yearsAfter you follow the answer(s) below, to drop the ".sh" you can rename the file:
mv example.sh example
and thenexample
should work on its own.
-
-
Rameez Hussain over 12 yearsHey thanks for your reply. I tried that out. Now I can run it without the "sh" command. But I still have to prefix the command with "./" which I don't want to. :)
-
Rameez Hussain over 12 yearsHey! Thanks for your reply. I tried using aliases earlier. But nothing happened. I placed it immediately after the "#!/usr/local/bin/bash" line. What do you think is wrong?
-
Rameez Hussain over 12 yearsI forgot to mention that I have already set the right permissions. I used the command "chmod 755 script.sh"
-
fge over 12 yearsSee comments about the path. You need to put your script in a directory which you have to append to your PATH.
-
ziesemer over 12 yearsAn alias can't be defined within your script - it has to be declared as part of your current environment. So just run the
alias
command at the shell prompt, and then the alias will be available to run the command with. -
Rameez Hussain over 12 yearsI dont know what the problem with the system is. I created a new shell script named "hello" with just an "echo "hello" " in it. I changed its permissions and tried ./hello. It dosn't work. it says-
-
jaypal singh over 12 yearsWhat changes have you made to your start up scripts?
-
Rameez Hussain over 12 yearsi tried sourcing the start up scripts. it says i dont have permissions to do so.
-
jaypal singh over 12 yearsThat doesn't look right. The start up scripts should be in your home directory. Make sure you are in your home directory when you source them.
-
Rameez Hussain over 12 yearsI dont seem to have permissions to do this. I checked the home folder. It has the .bashrc file. But if I try to source it it says "Permission denied"
-
jaypal singh over 12 yearsCan you update the permission of your .bashrc file? Try giving it a full permission for test purposes using
chmod 777 .bashrc
-
Rameez Hussain over 12 yearsI tried that, and tried sourcing it. Says I can't use the "." command. Then I tried editing the .bashcr file itself and then running the shell script. Nothing happened.
-
jaypal singh over 12 yearsif you have updated the .bashrc file and have added ur script directory to your path then just log out and log back in. The bachrc file gets sourced everytime you log in.
-
jordanm over 12 yearsYour home directory might be mounted with the noexec option, which will prevent you from executing it the way that you want to.
-
MycrofD about 7 yearsit didn't work in my case. but
source script.sh
worked fine, or rathersource <filename_without_any_extension_type>
-
Daniel Waltrip almost 7 years
source ./my-cool-script
(or. ./my-cool-script
) will pollute the current shell session with any variables defined in the script. Sourcing should not be used unless you want to explicitly modify the shell session by setting environment vars, changing the directory, etc. Really terrible bugs could result otherwise, as variables from one script execution, if done via sourcing, could affect another script execution. -
dstandish over 6 yearsIf you are sure the script folder is in PATH, and it still does not work without
./
, then you may have a name collision. Try renaming. -
AndrewD over 6 yearsyou'll still get a
command not found
error if you are in the script directory and not including./
. simply change to a different directory, and as long as the script is in your $PATH variable, it will work. -
tripleee almost 6 yearsYou should not put your own files in
/usr/bin
- many systems tave/usr/local/bin
for this purpose and reserve/usr/bin
strictly for the OS. -
RobertMcReed over 5 yearsRan into the same issue, and it was a naming collision as chorbs advised.
-
tripleee about 5 yearsThe parts of this answer that don't restate information from very old existing answers are confused.
-
Jay Sojitra about 5 yearsuse "alias demo = 'bash demo.sh'", then you don't have to write "./". Just simply write 'demo' and bang...
-
geckos almost 5 yearsIf you're receiving command not found the problem is not name collision, it would execute the wrong script/binary if was so. You may be setting you're path wrongly. You can
cd
to where the script is and issuePATH=$PATH:$PWD
it would work -
geckos almost 5 yearsAlso you have to set the PATH on the same
bash
session that you're executing. If you open a new terminal or another bash, or type bash on the same session your PATH is gone. If your script is trying to be executed by another command you may need to export it withexport PATH
so commands that run on your bash receives the up to date PATH -
Dani almost 5 yearswhat is the zsh shebang? How do I find the right path for zsh?
-
tripleee almost 5 years@Daniel
type zsh
wild print it if it's in yourPATH
. If you are already runningzsh
, you can also tryecho "$SHELL"
-
Dani over 4 years@tripleee thanks! not sure how I missed this reply...
which zsh
works too! -
Dani over 4 yearsHow can
chmod +x script.sh
be done via a shell script, so it doesn't have to be done manually for several files? -
tripleee over 4 years@Daniel Please post a new separate question instead. Nobody will see your comments down here except by accident, and you are drifting further and further off-topic. Also, I don't understand what you are asking.
-
tripleee over 4 years
myscripts
is unconventional and you spell it differently (singular vs plural) in different places. A more common arrangement is to call itbin
just like the directories for system binaries. -
tripleee over 4 yearsThere's no need to
sudo chmod
your own file. -
Dani over 4 yearsTo clarify (which I don't think is off-topic): "And make your file executable (chmod +x script.sh)." How can that be done in a shell script, so that running that script, executes that command, just like if one was to type it in Terminal?
-
Luca Di Liello over 4 yearsThere is no need to reboot the machine. At most you should source again
-
tripleee almost 4 yearsDon't use Bash-only syntax in
.profile
, which is shared with other shells.export PATH=value
is not portable, and should instead be broken up into two statements. Except of course you can almost certainly simply remove theexport
because surely, this variable will already beexport
ed for you, so there is no need to do that again. -
tripleee almost 4 years... Provided you also previously made sure the file is readable and executable for everyone.
chmod a+x quickcommit.sh
-
tripleee almost 4 yearsDon't do this. There are good reasons to not have the current directory on your
PATH
. It makes it too easy - especially for beginners - to accidentally shadow crucial system commands, or worse, to be tricked into executing somebody else's commands. -
wjandrea over 3 years@tripleee Re
myscript(s)
, I edited it, but rather than trying to fix the typo, I changed it tobin
. -
tripleee over 3 years@wjandrea Good initiative. I removed the
export
too. -
tripleee over 3 yearsI removed the
sudo
beforechmod
and the suggestion to reboot. -
Charles Duffy over 2 yearsAdding an alias per-executable is not good practice. Aliases can't be run except from at an interactive shell (they can't be called from other scripts or invoked from non-shell applications). There's no reason to have an alias at all if you install the executable at a location in the PATH.
-
Haroun Hajem about 2 yearsBest answer here. No need to fool around and do all the different steps.
-
midnite about 2 yearsOn my Android shell (which is LineageOS), the shell is /bin/sh, which I must use the
sh
command to run a shell script../
does not work. Of course it has the executable flag on and file starts with#!/bin/sh
.