LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 04-15-2013, 08:01 PM   #1
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302
Blog Entries: 7

Rep: Reputation: 44
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!
 
Old 04-15-2013, 08:35 PM   #2
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
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.
 
Old 04-16-2013, 12:08 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 04-16-2013, 10:09 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by Nbiser View Post
I know that just typing bash before the name of the script is much easier for me than changing its permissions!
yes, it can be a sloppy affair.
I use
Code:
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod a+x <afile>
in my ~/.vimrc so that any interpreter on
line(1) will cause vim to make the file "-rwxr-xr-x"

typing sucks.

Have fun.
 
1 members found this post helpful.
Old 04-16-2013, 12:00 PM   #5
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302

Original Poster
Blog Entries: 7

Rep: Reputation: 44
Quote:
Originally Posted by cortman View Post
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.
 
Old 04-16-2013, 01:00 PM   #6
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
Quote:
Originally Posted by Nbiser View Post
Yes but the vast majority of system tasks will be done in the terminal.
True, but an equal amount of system tasks will be done automatically by the system, for which you also need to set the executable permission.

Do whatever works for you (within reason).
 
Old 04-16-2013, 08:18 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Its actually less typing in the long run.
Consider:

1. create file : fixed amt of work
2. chmod file : fixed amt of work
3a. run using '/path/to/script'
3b. run as 'bash /path/to/script'

Note the extra typing for each usage via 3a. (EDIT: 3b darn it, not 3a)
The first line in the script should be the hash-bang line.

Last edited by chrism01; 04-22-2013 at 02:20 AM. Reason: fix typo
 
Old 04-21-2013, 04:58 PM   #8
Bill Melater
Member
 
Registered: Apr 2012
Posts: 31

Rep: Reputation: Disabled
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.

Enjoy.
 
Old 04-21-2013, 05:42 PM   #9
H5X00R
Member
 
Registered: Apr 2013
Posts: 39

Rep: Reputation: 1
@ Nbiser

If preceding bash before the script makes you happy go for it.

However, linux veterans probably do this with their shell scripts.
  • They chmod their script(s).
  • They test their script(s).
  • Put script(s) in a bin folder in their home directory
  • Add their bin folder to the $PATH variable

This method allows you to call the script from any directory you're in. No need to type bash. Thus, less tying.

Last edited by H5X00R; 04-21-2013 at 05:44 PM.
 
Old 04-22-2013, 02:20 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@Bill Melater
you missed the '!' each time eg
Code:
#!/bin/bash

#!/bin/ksh
etc ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 08:08 AM
[SOLVED] Making windows executable cygwin scripts sarenace Programming 10 05-11-2012 10:05 PM
encrypted executable scripts izghitu Linux - Security 7 11-04-2010 01:17 PM
Make a Bash script executable but not readable onesikgypo Linux - Newbie 7 09-08-2009 09:58 AM
Build scripts say my C compiler cannot make executable output noware Linux - Software 2 06-07-2006 02:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration