- How to make a file (e.g. a .sh script) executable, so it can be run from a terminal
- 4 Answers 4
- How to Make a File Executable in Linux
- How to make a file executable using the chmod command
- How to make a file executable using file properties
- Conclusion
- About the author
- Taimoor Mohsin
- Creating executable files in Linux
- 5 Answers 5
How to make a file (e.g. a .sh script) executable, so it can be run from a terminal
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?
It is not a duplicate, because I have asked specifically about making it application/x-executable. The other question just asks for opening sh file in terminal.
4 Answers 4
You can mark the file as executable:
You can then execute it like this:
If you want to use a different command to start it, you can add an alias:
Add this at the end of the file:
Open a new terminal session or type source ~/.bashrc in your terminal to apply. Then simply use the new name to start the script.
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.
@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.
There are two ways of making a file executable:
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.
Terminal / Command method:
chmod +x filename.extension
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
chmod -777 /path/to/file.extension
To take all permissions away from everyone.
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:
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
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!
How to Make a File Executable in Linux
Making bash script files executable is an efficient way to run your bash programs because by default the scripts are not executable. This is useful mainly for system administrators, as they need to develop several bash scripts daily to automate their tasks. For example, you might wish to run bash scripts to back up your work or log certain events on your server. So this article will guide you on how you can make your bash scripts executable, and for that, there are two main ways:
- Make a bash file executable using the CHMOD command
- Make a bash file executable using file properties
How to make a file executable using the chmod command
The “chmod” command stands for change mode, and this command can be used to make changes in the file permissions, such as you can make a file writable, readable, and executable. To see these permissions for any file, first, let’s create a bash script file in nano editor:
Then we have written some text and saved the file as shown below:
echo ”This is a test file to make it executable.”
Use the below-mentioned command to check whether the file is executable or not:
In the above image, the letter ‘r’ shows that the file is readable, and ‘w’ shows that the file is writeable and ‘x’ shows that the file is executable, which is currently missing on the above output, and that means you cannot execute this file for now. To verify this, you can execute this file by following the general syntax shown below.
As you can see, it shows an error after trying to execute the text file, and this is where the chmod command comes in handy. To make this file executable, all you need to do is to follow the general syntax shown below:
In the above image, ‘u+x’ shows that you are giving permission to the user to execute a specific file, and you can see that it added a letter ‘x’ in the image and after that, you need to run this bash file by typing.
It can be seen that the content of the bash file is now executed without any error.
How to make a file executable using file properties
You can also make a file executable by right-clicking on the bash file and selecting its ‘Properties’ as shown below:
The next step is to select the ‘Permissions’ tab and then check the option of ‘Allow executing file as program’.
This will also allow you to execute the bash file for which you have selected this option, upon double clicking the file, you will get the prompt as shown below:
You can either get output in the terminal or directly run the script for the desired result.
Conclusion
Making a bash script executable allows you to execute your bash scripts without having to type bash in the terminal. This is especially useful for system administrators, who need to write several bash scripts every day to automate their jobs. So in this article, we have taught you how you can make any bash file executable, and there are two ways for doing this. One is by using the “chmod” command, and the other is by using the file properties.
About the author
Taimoor Mohsin
Hi there! I’m an avid writer who loves to help others in finding solutions by writing high-quality content about technology and gaming. In my spare time, I enjoy reading books and watching movies.
Creating executable files in Linux
One thing I plan to be doing is writing (painfully simple) Perl scripts, and I’d like to be able to run them without explicitly calling Perl from the terminal. I appreciate that, to do this, I need to grant them execute permissions. Doing this with chmod is easy enough, but it also seems like a slightly laborious extra step. What I would like is one of two things: Firstly, is there a way to set the execute flag when saving a file? Currently I’m experimenting with gedit and geany, but would be willing to switch to a similarly- (or better-) featured editor if it had this capability. Failing that, is there a way to declare that all files created in a particular directory should have execute permissions? My umask is set to 022, which should be OK, as far as I understand, but it would appear that the files are created as text files (with 666 default permissions) rather than executable files (with 777 default permissions). Perhaps I’m just being lazy, but I figure there must be a more convenient way than chmodding every single script one creates.
You have to print the output of every file. You have to import the right libraries of every file. This seems like another step in the process of programming, and one that is dangerous to circumvent.
5 Answers 5
This should return something like
Then in the first line of your script add:
Then you can execute the file
There may be some issues with the PATH, so you may want to change that as well .
Thanks, but that’s not quite my problem. I’ve already gotten into the habit of starting my scripts with #!/usr/bin/perl. I can run scripts fine once I’ve given them executable permissions; I was just looking for a simpler way of doing so.
May I recommend #!/usr/bin/env perl instead? The env program basically finds the argument given (in this case, «perl») on the PATH and runs that. It’s useful when you’re sending scripts to other people — for example, I use a Mac, and Perl is located in /opt/local/bin.
No need to hack your editor, or switch editors.
Instead we can come up with a script to watch your development directories and chmod files as they’re created. This is what I’ve done in the attached bash script. You probably want to read through the comments and edit the ‘config’ section as fits your needs, then I would suggest putting it in your $HOME/bin/ directory and adding its execution to your $HOME/.login or similar file. Or you can just run it from the terminal.
This script does require inotifywait, which comes in the inotify-tools package on Ubuntu,
sudo apt-get install inotify-tools
Suggestions/edits/improvements are welcome.
#!/usr/bin/env bash # --- usage --- # # Depends: 'inotifywait' available in inotify-tools on Ubuntu # # Edit the 'config' section below to reflect your working directory, WORK_DIR, # and your watched directories, WATCH_DIR. Each directory in WATCH_DIR will # be logged by inotify and this script will 'chmod +x' any new files created # therein. If SUBDIRS is 'TRUE' this script will watch WATCH_DIRS recursively. # I recommend adding this script to your $HOME/.login or similar to have it # run whenever you log into a shell, eg 'echo "watchdirs.sh &" >> ~/.login'. # This script will only allow one instance of itself to run at a time. # --- config --- # WORK_DIR="$HOME/path/to/devel" # top working directory (for cleanliness?) WATCH_DIRS=" \ $WORK_DIR/dirA \ $WORK_DIR/dirC \ " # list of directories to watch SUBDIRS="TRUE" # watch subdirectories too NOTIFY_ARGS="-e create -q" # watch for create events, non-verbose # --- script starts here --- # # probably don't need to edit beyond this point # kill all previous instances of myself SCRIPT="bash.*`basename $0`" MATCHES=`ps ax | egrep $SCRIPT | grep -v grep | awk '' | grep -v $$` kill $MATCHES >& /dev/null # set recursive notifications (for subdirectories) if [ "$SUBDIRS" = "TRUE" ] ; then RECURSE="-r" else RECURSE="" fi while true ; do # grab an event EVENT=`inotifywait $RECURSE $NOTIFY_ARGS $WATCH_DIRS` # parse the event into DIR, TAGS, FILE OLDIFS=$IFS ; IFS=" " ; set -- $EVENT E_DIR=$1 E_TAGS=$2 E_FILE=$3 IFS=$OLDIFS # skip if it's not a file event or already executable (unlikely) if [ ! -f "$E_DIR$E_FILE" ] || [ -x "$E_DIR$E_FILE" ] ; then continue fi # set file executable chmod +x $E_DIR$E_FILE done