How do I run a shell script without using "sh" or "bash" commands?

609,725

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:

  1. Add the shebang line (#!/bin/bash) at the very top.
  2. Use chmod u+x scriptname to make the script executable (where scriptname is the name of your script).
  3. 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 your PATH variable.
  4. Run the script using just its name, scriptname.

If you don't have access to /usr/local/bin then do the following:

  1. Create a folder in your home directory and call it bin.

  2. Do ls -lA on your home directory, to identify the start-up script your shell is using. It should be either .profile or .bashrc.

  3. Once you have identified the start up script, add the following line:

    PATH="$PATH:$HOME/bin"
    
  4. 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

  5. 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
Share:
609,725

Related videos on Youtube

Rameez Hussain
Author by

Rameez Hussain

Updated on September 09, 2021

Comments

  • Rameez Hussain
    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
      Jonathan Leffler over 12 years
      The 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
      PJ Brunet over 7 years
      After you follow the answer(s) below, to drop the ".sh" you can rename the file: mv example.sh example and then example should work on its own.
  • Rameez Hussain
    Rameez Hussain over 12 years
    Hey 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
    Rameez Hussain over 12 years
    Hey! 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
    Rameez Hussain over 12 years
    I forgot to mention that I have already set the right permissions. I used the command "chmod 755 script.sh"
  • fge
    fge over 12 years
    See comments about the path. You need to put your script in a directory which you have to append to your PATH.
  • ziesemer
    ziesemer over 12 years
    An 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
    Rameez Hussain over 12 years
    I 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
    jaypal singh over 12 years
    What changes have you made to your start up scripts?
  • Rameez Hussain
    Rameez Hussain over 12 years
    i tried sourcing the start up scripts. it says i dont have permissions to do so.
  • jaypal singh
    jaypal singh over 12 years
    That 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
    Rameez Hussain over 12 years
    I 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
    jaypal singh over 12 years
    Can you update the permission of your .bashrc file? Try giving it a full permission for test purposes using chmod 777 .bashrc
  • Rameez Hussain
    Rameez Hussain over 12 years
    I 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
    jaypal singh over 12 years
    if 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
    jordanm over 12 years
    Your home directory might be mounted with the noexec option, which will prevent you from executing it the way that you want to.
  • MycrofD
    MycrofD about 7 years
    it didn't work in my case. but source script.sh worked fine, or rather source <filename_without_any_extension_type>
  • Daniel Waltrip
    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
    dstandish over 6 years
    If 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
    AndrewD over 6 years
    you'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
    tripleee almost 6 years
    You 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
    RobertMcReed over 5 years
    Ran into the same issue, and it was a naming collision as chorbs advised.
  • tripleee
    tripleee about 5 years
    The parts of this answer that don't restate information from very old existing answers are confused.
  • Jay Sojitra
    Jay Sojitra about 5 years
    use "alias demo = 'bash demo.sh'", then you don't have to write "./". Just simply write 'demo' and bang...
  • geckos
    geckos almost 5 years
    If 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 issue PATH=$PATH:$PWD it would work
  • geckos
    geckos almost 5 years
    Also 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 with export PATH so commands that run on your bash receives the up to date PATH
  • Dani
    Dani almost 5 years
    what is the zsh shebang? How do I find the right path for zsh?
  • tripleee
    tripleee almost 5 years
    @Daniel type zsh wild print it if it's in your PATH. If you are already running zsh, you can also try echo "$SHELL"
  • Dani
    Dani over 4 years
    @tripleee thanks! not sure how I missed this reply... which zsh works too!
  • Dani
    Dani over 4 years
    How can chmod +x script.sh be done via a shell script, so it doesn't have to be done manually for several files?
  • tripleee
    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
    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 it bin just like the directories for system binaries.
  • tripleee
    tripleee over 4 years
    There's no need to sudo chmod your own file.
  • Dani
    Dani over 4 years
    To 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
    Luca Di Liello over 4 years
    There is no need to reboot the machine. At most you should source again
  • tripleee
    tripleee almost 4 years
    Don'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 the export because surely, this variable will already be exported for you, so there is no need to do that again.
  • tripleee
    tripleee almost 4 years
    ... Provided you also previously made sure the file is readable and executable for everyone. chmod a+x quickcommit.sh
  • tripleee
    tripleee almost 4 years
    Don'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
    wjandrea over 3 years
    @tripleee Re myscript(s), I edited it, but rather than trying to fix the typo, I changed it to bin.
  • tripleee
    tripleee over 3 years
    @wjandrea Good initiative. I removed the export too.
  • tripleee
    tripleee over 3 years
    I removed the sudo before chmod and the suggestion to reboot.
  • Charles Duffy
    Charles Duffy over 2 years
    Adding 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
    Haroun Hajem about 2 years
    Best answer here. No need to fool around and do all the different steps.
  • midnite
    midnite about 2 years
    On 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.