There are two ways to run a shell script in Linux. You can use:

bash script.sh

Or you can execute the shell script like this:

./script.sh

That maybe simple, but it doesn’t explain a lot. Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script.

I am going to use this one line shell script to make things as uncomplicated as possible:

abhishek@itsfoss:~/Scripts$ cat hello.sh echo "Hello World!"

Method 1: Running a shell script by passing the file as argument to shell

The first method involves passing the script file name as an argument to the shell.

Considering that bash is the default shell, you can run a script like this:

bash hello.sh

Do you know the advantage of this approach? Your script doesn’t need to have the execute permission. Pretty handy for quick and simple tasks.

Run A Shell Script Linux
Running a Shell Script Linux

If you are not familiar already, I advise you to read my detailed guide on file permission in Linux.

Keep in mind that it needs to be a shell script that you pass as argument. A shell script is composed of commands. If you use a normal text file, it will complain about incorrect commands.

Running Text File As Script in Linux
Running a Text File As Script

In this approach, you explicitly specified that you want to use bash as the interpreter for the script.

Shell is just a program and bash is an implementation of that. There are other such shells program like ksh, zsh, etc. If you have other shells installed, you can use that as well instead of bash.

For example, I installed zsh and used it to run the same script:

Execute Shell Script With Zsh
Execute Shell Script With Zsh
.ugb-4037db6-wrapper.ugb-container__wrapper{border-radius:0px !important;padding-top:0 !important;padding-bottom:0 !important;background-color:#f1f1f1 !important}.ugb-4037db6-wrapper > .ugb-container__side{padding-top:35px !important;padding-bottom:35px !important}.ugb-4037db6-wrapper.ugb-container__wrapper:before{background-color:#f1f1f1 !important}.ugb-4037db6-content-wrapper > h1,.ugb-4037db6-content-wrapper > h2,.ugb-4037db6-content-wrapper > h3,.ugb-4037db6-content-wrapper > h4,.ugb-4037db6-content-wrapper > h5,.ugb-4037db6-content-wrapper > h6{color:#222222}.ugb-4037db6-content-wrapper > p,.ugb-4037db6-content-wrapper > ol li,.ugb-4037db6-content-wrapper > ul li{color:#222222}

Recommended Read:

.ugb-16406c5 .ugb-blog-posts__featured-image{border-radius:0px !important}.ugb-16406c5 .ugb-blog-posts__title a{color:#000000 !important}.ugb-16406c5 .ugb-blog-posts__title a:hover{color:#00b6ba !important}

How to Run Multiple Linux Commands at Once in Linux Terminal [Essential Beginners Tip]

How to Run Multiple Linux Commands at Once in Linux Terminal [Essential Beginners Tip]