How Do I Execute a Bash Script in Terminal?

How Do I Execute a Bash Script in Terminal?

I have a bash script like:

#!/bin/bash

echo Hello world!

How do I execute this in Terminal?

1

9 Answers

Yet another way to execute it (this time without setting execute permissions):

bash /path/to/scriptname

$prompt: /path/to/script and hit enter. Note you need to make sure the script has execute permissions.

1

cd to the directory that contains the script, or put it in a bin folder that is in your $PATH

then type

./scriptname.sh

if in the same directory or

scriptname.sh

if it's in the bin folder.

3

You could do:
sh scriptname.sh

11

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!

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.