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 Bash Script
The “Bourne again shell” command language processor is also known as Bash. The series of commands in a Bash script contains the plain text of the series of commands that are performed while the script is being run. The script must begin with “#!/bin/bash” in Bash. Several approaches for running a Bash script under Linux are used in this article.
How to Run a Bash Script
Bash is the GNU operating system’s shell or powerful scripting translator. With the use of Bash scripting, a user can neatly group the commands and examine them to prevent a potential damage to data, folders, and data structures. Your scripts’ coding conventions make it easier for other programmers to read the code and help in debugging. Although Bash scripting is not a language of programming, it is different which allows you to optimize the various tasks and work on administrative duties as well. Since other programming languages make it difficult to accomplish these tasks, Bash scripting is often preferred over other programming languages when using multiple tools at once. This is because the Bash script is extremely quick in these types of situations.
Let’s begin the process of opening the Bash script. To achieve this, we must first build a Bash script. Therefore, we create a Bash file on the desktop. Consequently, in the first step, we first open the terminal and change the directory to the desktop using the “cd” command. Then, we type “Desktop” and end it with a slash (“/”).
Now that we entered this command, we’re automatically taken to the desktop directory. To see how many files are now present on the desktop and see their names, we execute the “ls” command which shows all of the desktop’s files on the terminal window. We currently have four files on our desktop with the names “file.txt”, “name.txt”, “output.txt”, and “record.txt” as you can see in the following image. All of these files are text files and we need to open a Bash file so we first create a Bash script.
Creating a Bash File
Before constructing a Bash script, we demonstrate a few different Bash shells. To do this, type “cat” on the terminal, followed by the slash (“/”) and the “etc” term. We then write the slash (“/”) and the “shells” at the end.
As you can see in the following image, all the available shells are displayed in the terminal window when we type this command by pressing the enter key on the keyboard. The “/bin/sh”, “/bin/bash”, “/usr/bin/bash”, “/bin/rbash”, “/usr/bin/rbash”, “/bin/dash”, and “/usr/bin/dash” make up the total number of them, which is seven. Here, “/bin/bash” is the shell that we use. However, “/usr/bin/bash” is also an option.
Therefore, the “which bash” command now provides the path to the Bash shell when entered.
Now, when we enter this command, it provides the path which is “/usr/bin/bash” as seen in the following:
Now, in the subsequent section, we use the “touch” command to construct a Bash script. The main purpose of the “touch” command is to change the timeframe. Despite not being its main purpose, the tool frequently performs the file creation. The update and accessibility times for any specific file can be changed using the terminal software. The touch function only creates a file if one is not present. Thus, after typing “touch”, a blank space is provided before writing the name of the Bash script that we want to use. In this case, the Bash script’s name is “bashfile.sh” because it is a Bash script. So, the “.sh” extension is used.
As you can see in the following image, a file with the “bashfile.sh” name is created on the desktop when we run this command.
The text file named “bashfile.sh” is currently visible.
Adding a Bash Script
To make it a Bash script, we must follow a few steps. To do this, open the file from the desktop and type “#” followed by the exclamation mark “!” and a space. Then, type “/bin/bash”. This tells your system that the file is now a Bash script. Now that we have “echo”, we can input any information or element that we want to put in as a statement. Then, we use the inverted commas. Inside of these inverted commas, we type the statement for Bash which is “Linux is a family of open source Unix operating system”. We now save it by clicking the save option which is located at the top right.
To check the execution of the Bash script, we use the “ls” command in the following step:
When the command is performed, as shown in the following image, the Bash file is not executed because it is displayed in white color. In that case, the file is not yet executed. However, when it is displayed in green color, it indicates that the file is now executed.
There are two ways to modify the permission. However, in this case, we use the “chmod” command to make it executable in the following step. The “chmod” command is utilized to modify a file or directory’s access permissions. Its acronym is “change mode”. It cannot alter a symbolic link’s permissions. Even the symbolic links that we encounter during recursive directory traversal are disregarded. So, after typing “chmod”, we type “+x” to make the file executable before writing the “bashfile.sh” filename so there won’t be any errors when we run this command.
The “ls” command is now entered in the following step to check the file’s execution:
As you can see in the following image, after running this command, the file name “bashfile.sh” changed to green, indicating that it can now be executed.
Now, in the subsequent section, we run the Bash script. To do this, we use the command in which we type “./” followed by the name of the “bashfile.sh” Bash script.
Run the Bash Script
As you can see in the following image, when we run this command, the Bash script runs on the terminal screen which displays the “Linux is a family of open source Unix operating system” statement. We can see the results of the statement which we echoed here.
Using SH to Run a Bash Script
In this part, we run the Bash script on the terminal using “sh”. The “sh” is a system software task interpreter for Linux and Unix-like platforms. Some built-in commands are offered by sh. A subset of sh is Bash. Therefore, we use the command in which we write “sh” and then a space, followed by the name of the bash script which is “bashfile.sh”.
You can see that the Bash script is now successfully running when we input this command.
Using the Bash Keyword to Run a Bash Script
To run the Bash script on the terminal in this section, we utilize the “bash” keyword in the command. To do this, we enter “bash” followed by a space, then the script’s name which is “bashfile.sh”.
You can see that when we execute this command, it displays the statement we echoed which is “Linux is a family of open-source Unix operating systems”.
Conclusion
We discussed the topic on how to run a Bash script in Linux in this article. In the provided examples, we examined the alternate methodologies to run the Bash scripts. In the first stage, we discussed how to create a Bash file on the desktop and how to make it executable using the “chmod” command. In the second section, we used the “bash” and “sh” keywords to run the Bash script.
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.