How to create a shell script, and make it executable

This is a quick guide on how to create a shell script and make it executable. First off let us create a shell script. We could open up our favorite text editor in the GUI, and start typing, but that would require us to include as many of the Desktop Environments (DE) available as possible, so why don’t we just open the terminal which is both easier, and effectively the same throughout all distributions? I’ll just use vim, but you could use nano if you’d rather, or whatever text editor you’d prefer.

We must start our script with a shebang #! and then the absolute path to the sh command that will be responsible for interpreting the script we are about to create. This must be at the very first line of our script, without any spaces, or other characters whatsoever (otherwise it will just not work). So we should have this:

#!/bin/sh

Then we should add a new line, and start typing our script. In my case I will write something very simple, like to start Firefox. To do that we just have to add a new line and type firefox. So now we would have:

#!/bin/sh
firefox

Next we should save our script, and then make it executable. First we should give it a name, and then add the .sh extension. I will just name mine startff.sh Then, while still in the same directory as the file we created, we should run the following command:

chmod +x startff.sh

What we just did was to use the chmod command, which stands for change mode. Then we used the +x flag which stands for add execute permissions to the file, and then we specified what file we wanted to make executable. To learn more on this topic click here.

Lastly, we need to verify that it works. So we have to call the script we just created. Let us type the name of the file in the terminal, and see it in action, right? Oh-snap! We just got an error: bash: startff.sh: command not found

Don’t worry, we did nothing wrong but the last step. We are not supposed to run shell scripts like that. What we need to do instead is to add ./ in front of the script when we call it. So let us try again. This time type this:
./startff.sh

Lo and behold, you should see Firefox start. Great job! If not, it might not be installed. To learn how to install it follow this guide.

What would happen if we closed the terminal now, and switched to Firefox to browse the internet? Bad news! We can’t just open any program from the terminal like that, because it will remain attached to the terminal session. What this means is that when you close the terminal, it will close all child processes (programs) that we have started from within that session. To overcome this we need to detach the program we just executed. In this case we need to detach the Firefox instance from within our script.

Let us open the script again with our text editor, and introduce a space right after the firefox command and then add an ampersand (&). Therefore our script should now look like this:

#!/bin/sh
firefox &

After we save this, we can exit to the terminal again, and run it once more. This time Firefox would start, and immediately get detached from the terminal session. So we could now safely close the terminal, and our Firefox session would continue to exist. Cool, right?

Of course there are cooler things to do with shell scripts, but we should save that for a different post. For now we should just tap ourselves in the back, and watch dogs, or cat videos online.

Similar Posts