- How do I execute a bash script in Terminal?
- The Executing a Command Line Script Tutorial!
- Q: How do I execute this in Terminal?
- Confusions and Conflicts:
- The Path
- Extensions
- Changing directories
- Typing the program name
- SUDO
- Script location .
- A: To «execute this script» from the terminal on a Unix/Linux type system, you have to do three things:
- 1. Tell the system the location of the script. (pick one)
- 2. Tell the system that the script has permission to execute. (pick one)
- 3. Tell the system the type of script. (pick one)
- Run Bash Script Using sh
- Run Bash Script using bash
- Run Bash Script using source
- Run Bash Script by Specifying the Path
- Run Bash Script with Arguments
- Run Bash Script using the GUI
- Run Bash Script from Anywhere
How do I execute a bash script in Terminal?
Yet another way to execute it (this time without setting execute permissions):
had a seperate issue (trying to install anaconda via terminal on mac) and this was the solution. only thing I had to do was close and restart the shell, follow the one step here ( /(your conda installation path)/bin/conda init zsh ) and then close and reopen the shell one more time
$prompt: /path/to/script and hit enter. Note you need to make sure the script has execute permissions.
If you already are in the /path/to directory, e.g. with the cd /path/to command, you can enter ./script to run your script.Don’t forget, in this case the ‘./’ before ‘script’
cd to the directory that contains the script, or put it in a bin folder that is in your $PATH
if in the same directory or
./scriptname.sh works for me but scriptname.sh gives scriptname.sh: command not found . -rwxr-xr-x are its permissions.
The advice to cd anywhere at all perpetrates another common beginner misunderstanding. Unless the script internally has dependencies which require it to run in a particular directory (like, needing to read a data file which the script inexplicably doesn’t provide an option to point to) you should never need to cd anywhere to run it, and very often will not want to.
This is an old thread, but I happened across it and I’m surprised nobody has put up a complete answer yet. So here goes.
The Executing a Command Line Script Tutorial!
Q: How do I execute this in Terminal?
The answer is below, but first . if you are asking this question, here are a few other tidbits to help you on your way:
Confusions and Conflicts:
The Path
- Understanding The Path (added by tripleee for completeness) is important. The «path» sounds like a Zen-like hacker koan or something, but it is simply a list of directories (folders) that are searched automatically when an unknown command is typed in at the command prompt. Some commands, like ls may be built-in’s, but most commands are actually separate small programs. (This is where the «Zen of Unix» comes in . «(i) Make each program do one thing well.»)
Extensions
- Unlike the old DOS command prompts that a lot of people remember, you do not need an ‘extension’ (like .sh or .py or anything else), but it helps to keep track of things. It is really only there for humans to use as a reference and most command lines and programs will not care in the least. It won’t hurt. If the script name contains an extension, however, you must use it. It is part of the filename.
Changing directories
- You do not need to be in any certain directory at all for any reason. But if the directory is not on the path (type echo $PATH to see), then you must include it. If you want to run a script from the current directory, use ./ before it. This ./ thing means ‘here in the current directory.’
Typing the program name
- You do not need to type out the name of the program that runs the file (BASH or Python or whatever) unless you want to. It won’t hurt, but there are a few times when you may get slightly different results.
SUDO
- You do not need sudo to do any of this. This command is reserved for running commands as another user or a ‘root’ (administrator) user. Running scripts with sudo allows much greater danger of screwing things up. So if you don’t know the exact reason for using sudo , don’t use it. Great post here.
Script location .
# A good place to put your scripts is in your ~/bin folder. > cd ~/bin # or cd $HOME/bin > ls -l
You will see a listing with owners and permissions. You will notice that you ‘own’ all of the files in this directory. You have full control over this directory and nobody else can easily modify it.
If it does not exist, you can create one:
> mkdir -p ~/bin && cd ~/bin > pwd /Users/Userxxxx/bin
A: To «execute this script» from the terminal on a Unix/Linux type system, you have to do three things:
1. Tell the system the location of the script. (pick one)
# type the name of the script with the full path > /path/to/script.sh # execute the script from the directory it is in > ./script.sh # place the script in a directory that is on the PATH > script.sh # . to see the list of directories in the path, use: > echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # . or for a list that is easier to read: > echo -e $ # or > printf "%b" "$" /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
2. Tell the system that the script has permission to execute. (pick one)
# set the 'execute' permissions on the script > chmod +x /path/to/script.sh # using specific permissions instead # FYI, this makes these scripts inaccessible by ANYONE but an administrator > chmod 700 /path/to/script.sh # set all files in your script directory to execute permissions > chmod +x ~/bin/*
There is a great discussion of permissions with a cool chart here.
3. Tell the system the type of script. (pick one)
- Type the name of the program before the script. (Note: when using this method, the execute(chmod thing above) is not required
> bash /path/to/script.sh . > php /path/to/script.php . > python3 /path/to/script.py .
- Use a shebang, which I see you have ( #!/bin/bash ) in your example. If you have that as the first line of your script, the system will use that program to execute the script. No need for typing programs or using extensions.
- Use a «portable» shebang. You can also have the system choose the version of the program that is first in the PATH by using #!/usr/bin/env followed by the program name (e.g. #!/usr/bin/env bash or #!/usr/bin/env python3 ). There are pros and cons as thoroughly discussed here.
Note: This «portable» shebang may not be as portable as it seems. As with anything over 50 years old and steeped in numerous options that never work out quite the way you expect them . there is a heated debate. The most recent one I saw that is actually quite different from most ideas is the «portable» perl-bang:
#!/bin/sh exec perl -x "$0" "$@" #!perl
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.
Follow this tutorial to learn how to run a Bash script using various methods.
- 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:
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:
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:
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
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:
- The shorthand $HOME path requires the script to be in /home/ , regardless of your current location:
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.
./script.sh Hello bash script.sh Goodbye
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.
2. Select Preferences from the list.
3. Click the Behavior tab in the menu. Then, select Ask what to do in the Executable Text Files section.
4. Close the menu and double-click the script file. A prompt appears with several options.
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:
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.