Making a Bash Script File an Executable

Before a bash script can be executed, the system must recognize you having permission to execute the script. Every file has hidden flags that tells the system things like what kind of file it is and who/what has permission to access the file. The permissions are read, write, and execute. They are in three groups for the Owner, Group and All Users. You can read more about file permissions on my A Quick Look At Linux File Permissions page.

If you are not sure if the executable bit (flag) is set on a bash script just enter ls -l and the name of the bash script.. The ls is the list command and the -l parameter is for the long listing which shows more details such as the file permissions. Below you can see where I performed a ls -l helloworld for the helloworld bash script. You can see there is a bunch of letters at the first of the line. You will notice there is no letter x in the list. The r is for read permission, the w is for the write permission. You will notice there is no letter x in the list. This means helloworld isn't an executable.

$ ls -l helloworld

-rw-r--r-- 1 ongytenes ongytenes 58 Jan 27 14:35 helloworld

To give yourself and others permission to execute the script, you can use the chmod (change the file mode bits/flags) with the +x parameter. The +x parameter sets the helloworld execute permission.

$ chmod +x helloworld

Now when the ls -l is used you should see the following

-rwxr-xr-x 1 ongytenes ongytenes 58 Jan 27 14:35 helloworld

Notice the letters x means you now have permission to execute helloworld . On many Linux distros executables are shown with green text.

For more details see