291

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.

2
  • 1
    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. Commented Jan 8, 2012 at 18:28
  • 1
    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.
    – Jay Brunet
    Commented Dec 13, 2016 at 6:14

13 Answers 13

484

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)

17
  • 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. :) Commented Jan 8, 2012 at 18:26
  • 5
    See comments about the path. You need to put your script in a directory which you have to append to your PATH.
    – fge
    Commented Jan 8, 2012 at 18:33
  • 2
    it didn't work in my case. but source script.sh worked fine, or rather source <filename_without_any_extension_type>
    – MycrofD
    Commented Mar 22, 2017 at 14:28
  • 5
    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. Commented Jun 20, 2017 at 19:18
  • 3
    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.
    – dstandish
    Commented Sep 3, 2017 at 6:30
80

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.

14
  • 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- Commented Jan 8, 2012 at 19:42
  • What changes have you made to your start up scripts? Commented Jan 8, 2012 at 19:50
  • 1
    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
    Commented Nov 18, 2019 at 5:22
  • 1
    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
    Commented Aug 1, 2020 at 8:20
  • 1
    @wjandrea Good initiative. I removed the export too.
    – tripleee
    Commented Aug 29, 2020 at 7:25
23

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.

2
  • 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? Commented Jan 8, 2012 at 18:28
  • 1
    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.
    – ziesemer
    Commented Jan 8, 2012 at 18:39
23

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!

5
  • 4
    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.
    – tripleee
    Commented Jul 21, 2018 at 15:05
  • There's no need to sudo chmod your own file.
    – tripleee
    Commented Nov 18, 2019 at 5:25
  • 1
    There is no need to reboot the machine. At most you should source again Commented Dec 6, 2019 at 15:21
  • I removed the sudo before chmod and the suggestion to reboot.
    – tripleee
    Commented Aug 29, 2020 at 7:29
  • Best answer here. No need to fool around and do all the different steps. Commented Apr 8, 2022 at 15:18
15

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
1
  • I forgot to mention that I have already set the right permissions. I used the command "chmod 755 script.sh" Commented Jan 8, 2012 at 18:29
6

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 password

now can run without .sh and in any directory

1
  • ... Provided you also previously made sure the file is readable and executable for everyone. chmod a+x quickcommit.sh
    – tripleee
    Commented Aug 1, 2020 at 8:23
2

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(using chmod +x <File Name>).

1
  • 2
    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.
    – tripleee
    Commented Aug 1, 2020 at 8:26
0

Here is my backup script that will give you the idea and the automation:

Server: Ubuntu 16.04 PHP: 7.0 Apache2, Mysql etc...

# Make Shell Backup Script - Bash Backup Script
    nano /home/user/bash/backupscript.sh
        #!/bin/bash
        # Backup All Start
        mkdir /home/user/backup/$(date +"%Y-%m-%d")
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_rest.zip /etc -x "*apache2*" -x "*php*" -x "*mysql*"
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_apache2.zip /etc/apache2
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_php.zip /etc/php
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_mysql.zip /etc/mysql
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/var_www_rest.zip /var/www -x "*html*"
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/var_www_html.zip /var/www/html
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/home_user.zip /home/user -x "*backup*"
        # Backup All End
        echo "Backup Completed Successfully!"
        echo "Location: /home/user/backup/$(date +"%Y-%m-%d")"

    chmod +x /home/user/bash/backupscript.sh
    sudo ln -s /home/user/bash/backupscript.sh /usr/bin/backupscript

change /home/user to your user directory and type: backupscript anywhere on terminal to run the script! (assuming that /usr/bin is in your path)

0

Enter "#!/bin/sh" before script. Then save it as script.sh for example. copy it to $HOME/bin or $HOME/usr/bin
The directory can be different on different linux distros but they end with 'bin' and are in home directory cd $HOME/bin or $HOME/usr/bin
Type chmod 700 script.sh
And you can run it just by typing run.sh on terminal. If it not work, try chmod +x run.sh instead of chmod 700 run.sh

1
  • The parts of this answer that don't restate information from very old existing answers are confused.
    – tripleee
    Commented Mar 31, 2019 at 16:17
0

Make any file as executable


Let's say you have an executable file called migrate_linux_amd64 and you want to run this file as a command like "migrate"

  1. First test the executable file from the file location:
[oracle@localhost]$ ./migrate.linux-amd64 
Usage: migrate OPTIONS COMMAND [arg...]
       migrate [ -version | -help ]

Options:
  -source          Location of the migrations (driver://url)
  -path            Shorthand for -source=file://path 
  -database        Run migrations against this database (driver://url)
  -prefetch N      Number of migrations to load in advance before executing (default 10)
  -lock-timeout N  Allow N seconds to acquire database lock (default 15)
  -verbose         Print verbose logging
  -version         Print version
  -help            Print usage

Commands:
  goto V       Migrate to version V
  up [N]       Apply all or N up migrations
  down [N]     Apply all or N down migrations
  drop         Drop everyting inside database
  force V      Set version V but don't run migration (ignores dirty state)
  version      Print current migration version
  1. Make sure you have execute privileges on the file
    -rwxr-xr-x 1 oracle oinstall 7473971 May 18 2017 migrate.linux-amd64
    if not, run chmod +x migrate.linux-amd64

  2. Then copy your file to /usr/local/bin. This directory is owned by root, use sudo or switch to root and perform the following operation

sudo cp migrate.linux-amd64 /usr/local/bin
sudo chown oracle:oracle /user/local/bin/migrate.linux.amd64
  1. Then create a symbolic link like below
sudo ln /usr/local/bin/migrate.linux.amd64 /usr/local/bin/migrate
sudo chown oracle:oracle /usr/local/bin/migrate
  1. Finally add /usr/local/bin to your path or user profile
export PATH = $PATH:/usr/local/bin
  1. Then run the command as "migrate"
[oracle@localhost]$ migrate
Usage: migrate OPTIONS COMMAND [arg...]
       migrate [ -version | -help ]

Options:
  -source          Location of the migrations (driver://url)
  -path            Shorthand for -source=file://path 
  -database        Run migrations against this database (driver://url)
  -prefetch N      Number of migrations to load in advance before executing (default 10)
  -lock-timeout N  Allow N seconds to acquire database lock (default 15)
  -verbose         Print verbose logging
  -version         Print version
  -help            Print usage

Commands:
  goto V       Migrate to version V
  up [N]       Apply all or N up migrations
  down [N]     Apply all or N down migrations
  drop         Drop everyting inside database
  force V      Set version V but don't run migration (ignores dirty state)
  version      Print current migration version
0
  1. Make the script file as executable by using file's properties
  2. Create alias for the executable in ~/.bashrc. alias <alias namme> = <full script file path>'
  3. refresh the user session to apply it. source ~/.bashrc
1
  • 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. Commented Nov 1, 2021 at 21:18
0

Just to add to what everyone suggested. Even with those solutions, the problem will persist if the user wants to execute the script as sudo

example: chmod a+x /tmp/myscript.sh sudo ln -s /tmp/myscript.sh /usr/local/bin/myscript

typing myscript would work but typing sudo myscript would return command not found.

As sudo you would have to still type sudo sh myscript or sudo bash myscript.

I can't think of a solution around this.

1
  • You can enclose code fragments in "`"
    – gonidelis
    Commented Sep 27, 2022 at 13:56
-1

Just:

/path/to/file/my_script.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.