LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2014, 04:37 PM   #1
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Which shebang to use for portable shell scripts


I'm writing some shell scripts that are intended to be POSIX-compliant enough to work in BASH, DASH and ZSH.

What should I use for the shebang?

I assume it's the following, but I thought I'd ask here first:

Code:
#!/bin/sh
 
Old 11-23-2014, 04:43 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
#!/bin/sh is probably the best way to go, at least when bash is symlinked to /bin/sh and is called as that it will run in posix mode and disable any bash specific functions.
 
1 members found this post helpful.
Old 11-23-2014, 04:52 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by dugan View Post
I'm writing some shell scripts that are intended to be POSIX-compliant enough to work in BASH, DASH and ZSH.
What OSes are you targeting?
Are you sure at least one of the above mentioned shells will be available on them?
 
Old 11-23-2014, 06:42 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Original Poster
Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
I'm targeting Linux. If there's a Linux distribution that doesn't provide one of the three, then I don't need to support it.
 
Old 11-23-2014, 06:43 PM   #5
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Make sure they run on ash/dash, and then you can use the '#!/bin/sh', if they don't run because they need bash features then use '#!/bin/bash'. I would use:
http://sourceforge.net/projects/checkbaskisms/
with '-p' and maybe '-x' to ensure POSIX compliance.

Last edited by metaschima; 11-23-2014 at 06:44 PM.
 
1 members found this post helpful.
Old 11-24-2014, 12:06 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
From the shells I've tested, 'dash' was the most minimalistic (or most strict if you wish), (an example: it doesn't accept trap '' SIGHUP only trap '' HUP)
 
Old 11-24-2014, 12:55 AM   #7
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
#!/bin/sh should do what you need, i think the newer release supports arrays like features too, at least it worked when i tested on my test machine.
 
Old 11-25-2014, 02:09 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
@SAbhi You are missing the fact there is almost never a native /bin/sh under Linux. On all mainstream distributions, it is a link to a Bourne shell compatible implementation, commonly bash or dash, less frequently zsh or busybox.

Dash doesn't support array variables and probably never will (unless the POSIX standard decide to add this feature).
 
Old 11-25-2014, 02:39 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I am probably going to cop a flogging for this, but are there really that many distributions that do not contain a copy of bash?

I am aware that it may not necessarily be the system default, but I did not think this implied it was not available at all.

Looking at some main stream distributions like, Mint, Ubuntu, Debian, openSuse, Slackware, I was not able to find any that indicated bash was not included.
I am not saying that posix standards are not the way to go, however, I find the argument to steer away from bash on most modern distributions to not necessarily
be a good argument either.

Just my 2 cents of course

What I can say I have noted as a good practice of late, is that rather than setting the shebang directly is that you let env find it for you.
As an exmple:
Code:
#!/usr/bin/env bash

Last edited by grail; 11-25-2014 at 02:40 AM.
 
Old 11-25-2014, 07:48 AM   #10
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
As has been shown by recent shellshock bug, relying on a big piece of software when you don't have to may bite you in the ass. Also, there are some distributions that do not contain a copy of bash, for example all those based on busybox.
 
1 members found this post helpful.
Old 11-25-2014, 08:46 PM   #11
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
Originally Posted by jlliagre View Post
@SAbhi You are missing the fact there is almost never a native /bin/sh under Linux. On all mainstream distributions, it is a link to a Bourne shell compatible implementation, commonly bash or dash, less frequently zsh or busybox.

Dash doesn't support array variables and probably never will (unless the POSIX standard decide to add this feature).
On most of the systems, Yes it is bash and in many but less to former cases a link to DASH.
based on the fact DASH doesn't support arrays but bash does.
Sorry for not being illustrative in my previous post and thanks for correcting me.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 06:17 PM
How to get the shebang line to work in python scripts ? pascor Linux - Newbie 6 07-09-2012 07:11 PM
[SOLVED] Scripts need full path on shebang line; affects urxvt catkin Linux - Software 3 05-02-2011 11:48 AM
Init scripts shebang lincoln.runwal Linux - Newbie 1 03-25-2009 05:22 AM
Bash scripts are no longer portable? J_Szucs Linux - Software 1 12-09-2004 11:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 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