How To Run A Bash Script {7 Methods}

IntroductionBash 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.Follow this tutorial to learn how to run a Bash script using various methods. How to Run a Bash Script Prerequisites

  • 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 name> <arguments>

For example:

sh script.sh

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:

ls -l /bin/sh

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.

Run Bash Script using bash

To run a Bash script using bash, enter:

bash <script name> <arguments>

For example:

bash script.sh

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 <path>/<file name> <arguments>

Alternatively, use:

. <path>/<file name>

For example:

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:

chmod +x <file name>

Now, execute the script with:

<path to script>/<file name> <arguments>

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

  • The absolute path works regardless of the current location:
/home/kb/script.sh

run script from absolute path

  • The relative path requires the script to be in the current location:
./script.sh

run script from home folder

  • The shorthand $HOME path requires the script to be in /home/<user>, regardless of your current location:
~/script.sh

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.

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.For example:

./script.sh Hello
bash script.sh Goodbye

run script with arguments For multiple arguments, separate each by a space.

Run Bash Script using the GUI

To run scripts using the GUI, change the behavior of the file explorer before running the script.To do so:1. Open Files and click on the top-right icon. files icon files icon 2. Select Preferences from the list. files preferences 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:

vim ~/.bashrc

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

export PATH="<path to script>:$PATH"

For example:

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

Add the line at the very end of the file.3. Save the changes and close Vim:

:wq

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

source ~/.bashrc

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

script.sh

script.sh terminal output The method permanently adds the path for the script, making it available in any terminal session.ConclusionAfter 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.

原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/222436.html

(0)
上一篇 2022年1月6日
下一篇 2022年1月6日

相关推荐

发表回复

登录后才能评论