444

I have a script.sh file and type of this file is shellscript file. I want to make this file as application/x-executable file. How can I make it?

3

4 Answers 4

598

You can mark the file as executable:

chmod +x filename.sh

You can then execute it like this:

./filename.sh

If you want to use a different command to start it, you can add an alias:

gedit ~/.bashrc

Add this at the end of the file:

alias <new name>='/home/<full path to script>/filename.sh'

Open a new terminal session or type source ~/.bashrc in your terminal to apply. Then simply use the new name to start the script.

5
  • Do you know how to use sudo command after entering the command as: "alias command1 = '/home/user_name/dir/script.sh'. In mine, it works without sudo, but not with it.
    – Aditya
    Oct 25, 2016 at 9:05
  • 1
    You need to restart bash after editing the .bashrc run: exec bash to restart bash. Aug 31, 2017 at 10:09
  • 2
    Why does the executable file be ./filename.sh and not just filename.sh?
    – user1995
    Nov 3, 2017 at 18:48
  • @user1993 yes, I am also looking for a way to make it executable just by filename and not ./filename
    – MycrofD
    Nov 27, 2017 at 14:02
  • 2
    @user1993 Generally, using ./filename.sh specifies a file in the current directory and using filename.sh specifies a file in the current directory or any directory of PATH. The first usage removes any uncertainty as to which file is accessed. In this case, you are attempting to execute the script with bash or another interpreter (by virtue of assumed #!/bin/bash as first line in your script) just by entering the filename. This usage requires the directory is specified. Alternatively, you can try bash filename.sh which seems to work with unspecified directory.
    – a505999
    Feb 10, 2018 at 21:45
64

There are two ways of making a file executable:

GUI Method:

Right-click the file and select Properties. Go to the permissions tab, then tick the box Execute: [ ] Allow executing file as program or in Nautilus Program: [ ] Allow this file to run as a program in Thunar.

enter image description here

Terminal / Command method:

You can either use:

cd /to/my/required/directory

Then run

chmod +x filename.extension

Or just run:

chmod +x /path/to/your/filename.extension

chmod does also have some more advanced options:

The spaces are to show that it is split up: - rwx --- ---

The first set of --- is User. The second is Group and the last is Other (anyone else)

r stands for Read, w for Write and x for eXecute.

So to allow everyone to read it, but only Group to execute and User to read and write it (but for some reason not execute) would be:

-rw- rx- r-- But this would be added to the command as:

chmod +rw-rx-r-- /path/to/file.extension

chmod also can do this in numbers. It is based on binary (I think, as it is 1,2 and 4)

So there are these numbers:

Execute by user is 100. Execute by group is 010. Execute by other is 001.

Write by user is 200. Write by group is 020. Write by other is 002.

Read by user is 400. Read by group is 040. Read by other is 004.

Then you add these together to get the desired combination.

So to allow everyone to read it, but only Group to execute and User to write it (but for some reason not execute) would be:

400 + 040 + 004 and 010 and 200

That adds up to 600 + 050 + 004 = 654.

You could then run the command.

chmod +654 /path/to/file.extension to set it.

And to set all permissions you can type:

chmod +rwxrwxrwx /path/to/file.extension

Or (this is a bit easier to write, but harder to remember each one):

chmod +777 /path/to/file.extension

Finally, you can do:

chmod -777 /path/to/file.extension

To take all permissions away from everyone.

And:

chmod +300 /path/to/file.extension

To add read and write for user, without affecting any other permissions (e.g. Execute permissions).

This website has a very useful little grid checkbox thing, whereby you can tick the options you want and it gives you the command:

enter image description here

However, not all the possible combinations are sensible to use; the main ones that are used are the following:

755 - Owner has all, and Group and Other can read and execute

700 - Owner has all

644 - Owner can read and write, and Group and Other can read

600 - Owner can read and write

And, if you're using non-trivial user groups:

775 - Owner can read and write, and Group and Other can read

770 - Owner and Group have all, and Other can read and execute

750 - Owner has all, and Group can read and execute

664 - Owner and Group can read and write, and Other can just read

660 - Owner and Group can read and write

640 - Owner can read and write, and Group can read

777 and 666 are rarely used, except in /tmp.

Thanks Ilmari Karonen for pointing out the ones in common usage!

4
  • Does't it is based on octal (8-base) instead of binary (2-based)? In binary you can have only 0 and 1, while in octal you can have 0, 1, ... 6, 7
    – Justinas
    Jan 2, 2018 at 15:06
  • @Justinas binary in that 7 = 4+2+1 - 111 represents Read and Write and Execute.
    – Tim
    Jan 2, 2018 at 15:08
  • the chmod +101 style permission modifications is what I was looking for. Thanks for this.
    – JasonS
    Jul 28, 2020 at 20:12
  • An extremely thorough answer! Sep 19, 2021 at 15:23
9

Run:

chmod +x /path/to/file.sh

To make it un-executable, run:

chmod -x /path/to/file.sh

For example i created .sh file:

vi tester12.sh

After i write some code on vi editor, i'll exit from vi editor:

:wq!
chmod +x tester12.sh
./tester12.sh
0

Let all users run your script

As stated you can set the execution bit of the file to make it executable with chmod +x. But you can also use chmod a+x:

$ ll file_command.txt
-rw-rw-r-- 1 rick rick 17 Sep 19 08:55 file_command.txt

$ chmod a+x file_command.txt

$ ll file_command.txt
-rwxrwxr-x 1 rick rick 17 Sep 19 08:55 file_command.txt*

NOTES:

  • A script doesn't have to end with .sh it can end in .txt as shown here, or have no extension whatsoever.
  • Instead of just +x (only you can execute the script), use a+x so all users can execute the script.
  • The file name has a * appended to it to indicate it is executable. Also the file name changes color to green on most systems.

make script executable


Run a script without making it executable

You can still run a bash script without making it executable. For example:

$ echo "echo Hello World" > file_command.txt

$ cat file_command.txt
echo Hello World

$ bash file_command.txt
Hello World

NOTES:

  • First we create a script containing echo Hello World.
  • Then we confirm the script called file_command.txt is correct.
  • Lastly we run the script by calling bash and passing it the script name. The bashcommand is actually store as /bin/bash and it is an executable on all Ubuntu systems.
  • Creating a file of commands saves you from adding the shebang #!/bin/bash as the first line in a script.
  • Creating a file of commands saves you from making it executable with chmod +x or chmod a+x.
1
  • What do you mean by a "file of commands"? When exactly do we need the shebang, and when do we not?
    – Wassadamo
    Oct 27, 2022 at 3:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .