[wpseo_breadcrumb]

How to make a file (e.g. a .sh script) executable, so it can be run from a terminal

Solutons:


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.

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!

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

Related Solutions

What is D-Bus practically useful for?

dbus does exactly what you said: it allows two-way communication between applications. For your specific example you mentioned terminator. From terminator's man page, we see: --new-tab If this is specified and Terminator is already running, DBus will be used to...

How to check ‘mdadm’ RAIDs while running?

The point of RAID with redundancy is that it will keep going as long as it can, but obviously it will detect errors that put it into a degraded mode, such as a failing disk. You can show the current status of an array with mdadm --detail (abbreviated as mdadm...

What is a “toast notification”?

A Toast is a non modal, unobtrusive window element used to display brief, auto-expiring windows of information to a user. Android OS makes relatively heavy use of them. Here's an example of a Google Chrome toast notification on Mac OS X: A list of descriptions...

Which elliptic curve should I use?

You are misreading Bernstein and Lange's advice (admittedly, their presentation is a bit misleading, with the scary red "False" tags). What they mean is not that some curves are inherently unsafe, but that safe implementation of some curves is easier than for...

How can I find files that are bigger/smaller than x bytes?

Use: find . -type f -size +4096c to find files bigger than 4096 bytes. And : find . -type f -size -4096c to find files smaller than 4096 bytes. Notice the + and - difference after the size switch. The -size switch explained: -size n[cwbkMG] File uses n units of...

Relative imports in Python 3

Explanation From PEP 328 Relative imports use a module's __name__ attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to '__main__') then relative imports are...

How to add a class to a given element?

If you're only targeting modern browsers: Use element.classList.add to add a class: element.classList.add("my-class"); And element.classList.remove to remove a class: element.classList.remove("my-class"); If you need to support Internet Explorer 9 or lower: Add...

less searches are always case-insensitive

I'm not sure how to enable this from the command line but when you're inside of less you can toggle the behavior you want by giving the -i command to less. toggling -i                searching for /blah and /BLAH               searching for /Blah       ...

Is using nested try-catch blocks an anti-pattern?

This is sometimes unavoidable, especially if your recovery code might throw an exception. Not pretty, but sometimes there are no alternatives. I don't think its an antipattern, just widely misused. Most nested try catch's are indeed avoidable and ugly as hell,...

Create a branch in Git from another branch

If you like the method in the link you've posted, have a look at Git Flow. It's a set of scripts he created for that workflow. But to answer your question: git checkout -b myFeature dev Creates the MyFeature branch off dev. Do your work and then git commit -am...

How can I set customise settings for htop?

htop has a setup screen, accessed via F2, that allows you to customize the top part of the display, including adding or removing a "Load average" field and setting it's style (text, bar, etc.). These seem to be auto saved in $HOME/.config/htop/htoprc, which...

Is there any way to manually bring up the keyboard?

As I see an alternative keyboard may solve your issue, and this seems to be an acceptable solution, and you even mention something you cannot find -- hereby I proudly present: Hacker's Keyboard Checking its Guide, there's in fact a section suggesting such a...

How to get rid of “No match found” when running “rm *”

This behaviour is controlled by several of Zsh's globbing options. By default, if a command line contains a globbing expression which doesn't match anything, Zsh will print the error message you're seeing, and not run the command at all. You can disable this in...

How to append date to backup filename

This isn't working because the command date returns a string with spaces in it. $ date Wed Oct 16 19:20:51 EDT 2013 If you truly want filenames like that you'll need to wrap that string in quotes. $ touch "foo.backup.$(date)" $ ll foo* -rw-rw-r-- 1 saml saml 0...

What does __all__ mean in Python?

Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the following code in a foo.py...