How to run linux bash script

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:

  1. Using bash or sh.
  2. Using source.
  3. 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.

Источник

How To Run A Bash Script

Bash scripts are programs that help automate tasks. Scripts store commands that often go together, such as updates and upgrades, to accomplish certain tasks automatically. After creating such programs, there are various ways to run the file and execute the commands through the command line or terminal.

Читайте также:  Linux going to root directory

Follow this tutorial to learn how to run a Bash script using various methods.

How to Run a Bash Script

  • Access to the command line/terminal (CTRL+ALT+T).
  • Essential Linux terminal commands. If you need a refresher, check out our Linux commands cheat sheet.
  • A Bash script file.
  • A text editor, such as Vi/Vim or Nano.

Run Bash Script Using sh

To run a Bash script using sh , enter the following command in the terminal:

sh script.sh terminal output

The sh is known as the Bourne shell, and it was the default command line interpreter for older systems. Although not that popular anymore, modern Unix-like systems include the interpreter under /bin/sh.

However, the /bin/sh often links to a different interpreter. To check which interpreter sh uses, run the following command:

sh link dash terminal output

The output shows the symbolic link for the sh interpreter. Commonly, Debian and Debian-based systems (such as Ubuntu) link sh to dash, whereas other systems link to bash.

Note: Refer also to our guide that explains how the Bash read command works through various examples.

Run Bash Script using bash

To run a Bash script using bash , enter:

bash script.sh terminal output

The bash interpreter is an acronym of Bourne Again Shell and a replacement for the Bourne Shell ( sh ). The interpreter resides in /bin/bash.

Run Bash Script using source

The source command executes commands from a specified file, including Bash scripts. The general syntax is:

source script.sh . script.sh 

run bash script from source terminal output

The path parameter is optional. In that case, the command searches through the directories in the $PATH environment variable. However, if the file is not in $PATH , the command searches the current directory for the file.

Use this method for running Bash scripts within other Bash scripts.

Note: Check out our post on Bash declare statement to learn how to work with variables and their attributes.

Run Bash Script by Specifying the Path

Another way to run a script is to specify the path. This method requires the file’s permission to be executable. Without changing the access rights, you get a Permission denied error.

To make a file executable, run the following command in the terminal:

Now, execute the script with:

Different path syntaxes assume the script’s location or your current location:

run script from absolute path

run script from home folder

  • The shorthand $HOME path requires the script to be in /home/ , regardless of your current location:

run script in home folder

Changing the permissions to executable and specifying the path is the most common and versatile method for running Bash scripts.

Note: Learn how to write Bash for loop scripts to save time with repeating processes.

Читайте также:  Установка gtk linux mint

Run Bash Script with Arguments

If a Bash script requires arguments, provide the argument after the script name. All methods for running scripts from the terminal accept user input arguments in the same way.

./script.sh Hello bash script.sh Goodbye 

run script with arguments

For multiple arguments, separate each by a space.

Note: Learn how to increment or decrement bash variables during script execution.

Run Bash Script using the GUI

To run scripts using the GUI, change the behavior of the file explorer before running the script.

1. Open Files and click on the top-right icon.

files icon

2. Select Preferences from the list.

files preferences

3. Click the Behavior tab in the menu. Then, select Ask what to do in the Executable Text Files section.

preferences ask what to do option

4. Close the menu and double-click the script file. A prompt appears with several options.

run script in terminal gui

Select the Run in Terminal option to run the script.

Note: If the terminal closes immediately, add $SHELL to the end of the script to keep the program open.

Run Bash Script from Anywhere

To run a script from anywhere, follow the instructions below.

1. Open the .bashrc file using a text editor:

2. Export the script’s path to the $PATH environment variable by adding the following line to the .bashrc file:

export PATH="/home/kb/scripts/:$PATH"

Add the line at the very end of the file.

4. Source the .bashrc file to apply the updates:

5. Lastly, run the script in the terminal from any location by stating the script’s name:

script.sh terminal output

The method permanently adds the path for the script, making it available in any terminal session.

Note: Learn more about Bash by referring to our post on the Bash let command used for evaluating arithmetic expressions.

After reading this guide, you know how to run a Bash script using various methods. Check out the best Linux text editors to start coding your scripts or learn how to write Bash functions.

If a shell script runs into problems during its execution, resulting in an error signal that interrupts the script unexpectedly it may leave behind temporary files that cause trouble when a user restarts the script. Using Bash trap Command, you can ensure your scripts always exit predictably.

Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.

Comments are an essential part of programming. Learn how to use comments and the best practices to apply to your Bash script commenting techniques.

Источник

Оцените статью
Adblock
detector