- How do I run .sh scripts?
- 16 Answers 16
- How to Run a Shell Script in Linux [Essentials Explained for Beginners]
- Method 1: Running a shell script by passing the file as argument to shell
- Method 2: Execute shell script by specifying its path
- That ./ before the script is important (when you are in the same directory as the script)
- Why most shell scripts contain #! /bin/bash at the beginning of the shell scripts?
- Was it helpful?
- How To Run Bash Script In Linux?
- Using bash or sh
- Using source
- By specifying the path to the script and chmod
How do I run .sh scripts?
Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can’t find any option similar to Right Click → Open With → Other Application. → Terminal. How do I open this file in the terminal?
You shouldn’t use extensions on scripts. At some point in the future, you may find that a different language is more suitable to do the task your current script is doing. And then you have a problem. Do you keep the old name, with a completely misleading extension, or do you rename it, possibly having to edit alot of places where your script is used?
You don’t need the file extension. It’s nice to have but is not needed. The OS doesn’t look at the file extension. It looks at the data
16 Answers 16
Give execute permission to your script:
chmod +x /path/to/yourscript.sh
Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:
Actually, you can use . /path/to/yourscript.sh if the script have to set up some environment variables.
Nobody mentions the traditional: ./path/to/yourscript.sh (without the space after . )? I find that one is the simplest and easiest to use. But anyways, here is my alternative that should do almost the same as ./ would, though I don’t see why you wouldn’t use ./ : (FILENAME=~/rem4space.sh;SLL=$(cat $FILENAME|head -1|sed ‘s:^#!\(.*\):\1:g’);[ ! -z $SLL ] && exec $SLL $FILENAME;sh $FILENAME) . edit FILENAME to your liking. Also note that sh will be used if there is no alternative.
You need to mark shell scripts as executable to run them from the file manager:
- Right click on your .sh file and select Properties:
- In the Permissions tab, check Allow executing file as program:
- Close the Properties window and double-click the file. A dialog will pop up giving you the option to run the script in a terminal:
This isn’t working in Ubuntu 13.04. Keeps opening in gedit anyway, never asks me to execute. Edit: Nvm, imjustmatthew answers this.
Before using this we need to make the file permission for execute using chmod. chmod +x filename.sh or chmod 755 filename.sh
How to Run a Shell Script in Linux [Essentials Explained for Beginners]
Here are all the essential details you should know about executing a shell script in the Linux command line.
That maybe simple, but it doesn’t explain a lot. Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script. I am going to use this one line shell script to make things as uncomplicated as possible:
[email protected]:~/Scripts$ cat hello.sh echo "Hello World!"
Method 1: Running a shell script by passing the file as argument to shell
The first method involves passing the script file name as an argument to the shell. Considering that bash is the default shell, you can run a script like this:
Do you know the advantage of this approach? Your script doesn’t need to have the execute permission. Pretty handy for quick and simple tasks. If you are not familiar already, I advise you to read my detailed guide on file permission in Linux. Keep in mind that it needs to be a shell script that you pass as argument. A shell script is composed of commands. If you use a normal text file, it will complain about incorrect commands. In this approach, you explicitly specified that you want to use bash as the interpreter for the script. Shell is just a program and bash is an implementation of that. There are other such shells program like ksh, zsh, etc. If you have other shells installed, you can use that as well instead of bash. For example, I installed zsh and used it to run the same script:
Method 2: Execute shell script by specifying its path
The other method to run a shell script is by providing its path. But for that to be possible, your file must be executable. Otherwise, you’ll have “permission denied” error when you try to execute the script. So first you need to make sure that your script has the execute permission. You can use the chmod command to give yourself this permission like this:
Once your script is executable, all you need to do is to type the file name along with its absolute or relative path. Most often you are in the same directory so you just use it like this:
If you are not in the same directory as your script, you can specify it the absolute or relative path to the script:
That ./ before the script is important (when you are in the same directory as the script)
Why can you not use the script name when you are in the same directory? That is because your Linux systems looks for the executables to run in a few selected directories that are specified in the PATH variable. Here’s the value of PATH variable for my system:
[email protected]:~$ echo $PATH /home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
- /home/abhishek/.local/bin
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
- /usr/games
- /usr/local/games
- /snap/bin
The binaries or executable files for Linux commands like ls, cat etc are located in one of those directories. This is why you are able to run these commands from anywhere on your system just by using their names. See, the ls command is located in /usr/bin directory.
When you specify the script WITHOUT the absolute or relative path, it cannot find it in the directories mentioned in the PATH variable.
Why most shell scripts contain #! /bin/bash at the beginning of the shell scripts?
Remember how I mentioned that shell is just a program and there are different implementations of shells.
When you use the #! /bin/bash, you are specifying that the script is to run with bash as interpreter. If you don’t do that and run a script in ./script.sh manner, it is usually run with whatever shell you are running.
Does it matter? It could. See, most of the shell syntax is common in all kind of shell but some might differ.
For example, the array behavior is different in bash and zsh shells. In zsh, the array index starts at 1 instead of 0.
Using #! /bin/bash indicates that the script is bash shell script and should be run with bash as interpreter irrespective of the shell which is being used on the system. If you are using zsh specific syntax, you can indicate that it is zsh script by adding #! /bin/zsh as the first line of the script.
The space between #! /bin/bash doesn’t matter. You can also use #!/bin/bash.
Was it helpful?
I hope this article added to your Linux knowledge. If you still have questions or suggestions, please leave a comment.
Expert users can still nitpick this article about things I missed out. But the problem with such beginner topics is that it is not easy to find the right balance of information and avoid having too much or too few details.
If you are interested in learning bash script, we have an entire Bash Beginner Series on our sysadmin focused website Linux Handbook.
If you want, you may also purchase the ebook with additional exercises to support Linux Handbook.
How To Run Bash Script In Linux?
The true power of a Bash script is utilized when it is run. But how to do that? Well, there are a plethora of ways to run a Bash script( shell script). Some of them may be useful in certain conditions, while it doesn’t matter how you run the script. Bash scripts are usually executed in the terminal or command-line interface.
To run a Bash script there are many ways. Some of them are given below:
- Using bash or sh.
- Using source.
- Running directly in a bash environment.
For making some of these methods work, the script must have a shebang as the header to indicate it’s a shell script or bash script in this case. So, be sure to include the command below at the top of the file.
This command will make the script run under the bash interpreter. It is recommended to write the shebang header even if it works without them.
Using bash or sh
This is the most standard way of executing the bash script. You must have git bash installed if you are using Windows. For Linux and macOS, bash is installed by default. In this method, we type bash followed by the file name with extension i.e. sh in this case. In a terminal, run the following code by replacing the filename with your bash script filename.
Here, bash is a program that contains the shell environments necessary to run the script from the bash shell. So this will execute the script from the bash interpreter.
Using bash command to run the script.
We can also use sh to run the script as it will direct to the default shell in the setup environment.
Using the sh command to run the bash script.
From the above example, we were able to run a bash script using bash as well as the sh command. If you are not in the same folder/directory as the script, make sure you specify the relative path to the script.
Using source
This method is quite easy to run a bash script, and all of them are quite simple. We just need to type in “source” before the file/script name with an extension. In a terminal, run the following code by replacing the filename with your bash script filename.
The script will simply get executed after “sourcing” the file. The source command will execute the shell script as the default bash command provided you are in the bash shell. You need to be in the bash shell to execute the script using the source command.
Using Source to run a bash script
From the screenshot of the script running, we can see that the source works exactly like the bash or sh command. The above script is a very basic script, but that doesn’t matter as long as the script is errorless and bug-free. Also, you need to add the relative path here as well if you are not in the same directory as the bash script.
By specifying the path to the script and chmod
This is a standalone method to run a bash script. We have to execute the script as an executable, we can run the script anywhere provided we have a bash shell somewhere in the environment. To make it executable we need to make sure we have the rights to run the file as an executable. We will use chmod for changing the rights on the file/script. In a terminal, run the following code by replacing the filename with your bash script filename.
The above command will allow us to execute the file. So it changes the mode of the file, the file should be read-only, executable, or any other mode for files. If you are using Linux and are not the root user, simply use sudo before the command chmod. The +x command will make sure the file is executable by everyone in the environment.
After the permission of the file is taken care of, we can now simply execute the file as follows. The command below takes into consideration that you are in the same directory as the file/ bash script is in.
If you are not on the same path as the bash script, make sure you provide the relative path to the file or the bash script.
using chmod and executing the script.
Executing a script from a relative path.
The above snippets and screenshots show that we can run the scripts in a bash environment by changing the mode of the file using the chmod.
From the following guide, we were able to run scripts in Linux using various methods and programs. So, those were some methods to run a bash script on Linux or pretty much anywhere.