[SOLVED] Why are you supposed to make bash scripts executable?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why are you supposed to make bash scripts executable?
Hi everybody!
I am trying to learn bash scripting and have always been puzzled by the fact that all of the guides say to use this code before executing:
Code:
chmod +x filename
. I have always skiped this step and gone on to run my program like so:
Code:
bash filename
. What not skip the chmod command and do it a simpler way? I know that just typing bash before the name of the script is much easier for me than changing its permissions!
Because it's sloppy. And if you write a script that gets called by the system instead of you invoking it, it will need to have the executable permission.
And in most GUI file managers you won't be able to run a script by clicking it unless it is set to +x.
lets look at it this way. In linux you have three basic permissions (not getting into owner,group,any)
you have r = READ, if you want to be able to touch this file you must have read permissions.
you have w = WRITE, if you wish to change or modify the file you must have write permissions.
you have x = EXECUTE, well duh if you want to execute, run, something it must be set to have EXECUTE permissions.
as cortman mentioned above if you ever wish the script to be run by the OS or via GUI it must have the proper permissions.
say for example you create a backup script to run nightly at 2am local on your system. In order to do this you use a combination of proper permissions a+x and you set it to either roots or users crontab depending on what level of backup you are performing. If both points are not met, your script will fail.
Because it's sloppy. And if you write a script that gets called by the system instead of you invoking it, it will need to have the executable permission.
And in most GUI file managers you won't be able to run a script by clicking it unless it is set to +x
Yes but the vast majority of system tasks will be done in the terminal. But, I can see what you mean, it is sloppy. Oh well, old habits are hard to break.
Everyone else has pretty much explained it to you. But did you know if you put the number sign followed by the shell you want to execute a script on the first line of the script file it will use that shell to execute it regardless of your command line shell interpreter.
For instance, first line of script is:
#/bin/sh
The OS will run this script with the /bin/sh.
Or:
#/bin/csh
The OS will run the script with /bin/csh
Or:
#/bin/ksh
The OS will use /bin/ksh.
Etc. Otherwise, without this specification, the OS will use your default command line shell. So, to make a script more portable, it is a good habit to specify which shell to use. Just a pointer about scripts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.