SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-09-05 04:15:17

flaneur
#! Member
Registered: 2014-01-24
Posts: 99

Unnecessarily using Bash instead of sh?

I came across this from HoaS:

I can't see any bashisms in those scripts (unless I've missed something) -- they would run quicker and consume fewer resources if you used a /bin/sh shebang (in Debian at least).

over here: http://crunchbang.org/forums/viewtopic. … 57#p438457.

I'm one of those who use #!/usr/bin/env bash because I don't know better. I guess I could work out, by trial and error, whether my scripts will work with #! /bin/sh but are there any guidelines or pointers about when #! /bin/sh will suffice? I guess it's not a big deal for small scripts or "modern" computers but I feel its worth it anyway.

Here's one that seems to require Bash:

#!/usr/bin/env bash

# hint: use "xdotool getmouselocation" to get values for your situation

xgg="$(xdotool getactivewindow getwindowname)"

if [[ "$xgg" == *" - LibreOffice Calc" ]]
  then
    xdotool mousemove --sync 218 144 click 1
fi

I think it's because of the [[ and ]]

(I use this script in LibreOffice Calc to sum contiguous cells without resorting to actually using the mouse; it works only on maximized windows or those snapped to the left and maximized vertically.)

Offline

Help fund CrunchBang, donate to the project!

#2 2015-09-05 08:04:59

ostrołęk
CB Member
Registered: 2015-08-13
Posts: 110

Re: Unnecessarily using Bash instead of sh?

What is the difference in  /bin/sh, /usr/bin/env bash, /usr/bin/bash and /bin/bash?
In Debian bash is available only in /bin, not in /usr/bin, whereas sh is a link to dash and that too is only available in /bin.

What is better, bash or dash?

Offline

#3 2015-09-05 09:31:30

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Re: Unnecessarily using Bash instead of sh?

Use this to check for bashisms (if you can get SourceForge to work...):
http://sourceforge.net/projects/checkbaskisms/

DASH is a stripped-down, minimalist shell (the Debian Almquist SHell, derived from ash) and is quicker and lighter than BASH.

As ostrolk points out, in Debian /bin/sh is symlinked to DASH whereas most non-Debian-based distributions symlink /bin/sh to BASH.

Preferring to use /bin/sh with modern machines and small scripts is unlikely to make any practical difference and is ultimately a matter of principle, similar to objecting to the use of `cat $FILE | grep foo` when `grep foo $FILE` would have the same result.

Offline

#4 2015-09-05 09:37:34

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

Re: Unnecessarily using Bash instead of sh?

it's mostly a question of portability i think.

on my system, /usr/bin/sh is just a link to bash, and /bin is just a link to /usr/bin.
so it doesn't really matter whether i use #!/bin/bash or "!/usr/bin/sh or any other combo. (*) (**)

what matters is that all my scripting experience is in bash, and i wouldn't even be able to say if my script would run on another shell.
so, to be on the safe side, i use #!/bin/bash.

(*) please correct me if i'm wrong

(**) ostrolk, on debian these things are a little different. but, at least for the question asked here, it shouldn't matter whether bash is in /bin or /usr/bin, as long as it's in your $PATH.
about env: try "man env".

Offline

#5 2015-09-05 09:42:22

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Re: Unnecessarily using Bash instead of sh?

ohnonot wrote:

on my system, /usr/bin/sh is just a link to bash, and /bin is just a link to /usr/bin.

Yeah, Arch doesn't even include DASH as standard so the symlink is to BASH.

For my Arch system I switched it to DASH by running:

# ln -sfT dash /bin/sh

https://wiki.archlinux.org/index.php/Da … ault_shell

I can't honestly say I have noticed the difference (I had to re-symlink it to BASH when installing Linux From Scratch and I forgot to switch it back for quite a while) but it makes me feel better.
smile

Offline

#6 2015-09-05 10:20:56

ostrołęk
CB Member
Registered: 2015-08-13
Posts: 110

Re: Unnecessarily using Bash instead of sh?

ohnonot wrote:

it's mostly a question of portability i think.

on my system, /usr/bin/sh is just a link to bash, and /bin is just a link to /usr/bin.
so it doesn't really matter whether i use #!/bin/bash or "!/usr/bin/sh or any other combo. (*) (**)

what matters is that all my scripting experience is in bash, and i wouldn't even be able to say if my script would run on another shell.
so, to be on the safe side, i use #!/bin/bash.

(*) please correct me if i'm wrong

(**) ostrolk, on debian these things are a little different. but, at least for the question asked here, it shouldn't matter whether bash is in /bin or /usr/bin, as long as it's in your $PATH.
about env: try "man env".

I'm sure it matters. In Debian, neither sh, dash nor bash is available in /usr/bin, so it should be either /bin/sh, /bin/bash or /bin/dash. This sh is just a link to dash. In reality, there is no such thing called sh. In Arch, /bin is just a link to /usr/bin, so you can use either /bin/bash or /usr/bin/bash.

I changed the scripts in the other post, http://crunchbang.org/forums/viewtopic.php?pid=438457 just to check to #! /bin/sh and #! /bin/dash and all worked. In Arch, it is straightforward and simple.

I wonder why there are so many links in Debian, to /bin, to /usr/bin, to /etc/alternatives and so on. It is somewhat muddling, like trying to touch your nose around the head.

Offline

#7 2015-09-05 10:35:06

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

Re: Unnecessarily using Bash instead of sh?

ostrolk wrote:

It is somewhat muddling, like trying to touch your nose around the head.

the downside of community-created software.

[ but then, who knows what twists & knots are hidden deep in the muddy bowels of prioprietary operating systems... ]

Offline

#8 2015-09-05 10:50:51

twoion
Moderator
Registered: 2012-05-11
Posts: 1,648

Re: Unnecessarily using Bash instead of sh?

It can make sense to use dash (or another simpler sh) instead of bash or definitely zsh when you spawn lots of shell instances that do nothing but to execute POSIX-compliant constructs like pipelines, or plain commands. It spawns faster because it's simply smaller with less context to set up.

Depending on how fast your computer is you might not notice a difference. I do, on a 2.7Ghz dualcore i5 laptop in 2015.


Tannhäuser ~ {www,pkg,ddl}.bunsenlabs.org/{gitlog,repoidx}

Offline

#9 2015-09-05 14:15:23

tknomanzr
#! Die Hard
From: Heavener, OK
Registered: 2014-12-09
Posts: 777

Re: Unnecessarily using Bash instead of sh?

Avoiding bashisms is important if you are trying to schedule cron jobs. That said, I generally just provide a wrapper script that does the trick for anything that I would end up needing to refactor the code to be sh compliant.

Offline

#10 2015-09-05 15:52:00

ostrołęk
CB Member
Registered: 2015-08-13
Posts: 110

Re: Unnecessarily using Bash instead of sh?

ohnonot wrote:
ostrolk wrote:

It is somewhat muddling, like trying to touch your nose around the head.

the downside of community-created software.

[ but then, who knows what twists & knots are hidden deep in the muddy bowels of prioprietary operating systems... ]

There is no real thing called sh, but a link to dash, which is a real thing. So, we have only bash and dash. So, the linking is just running around from one place to another. You can simply write #! /bin/dash without the unnecessary #! /bin/sh

Check for example how you open Iceweasel. How many links to open? How many times the comand goes around /usr/bin?

Offline

#11 2015-09-05 15:55:59

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Re: Unnecessarily using Bash instead of sh?

ostrolk wrote:

You can simply write #! /bin/dash without the unnecessary #! /bin/sh

As ohnonot points out, portability is an issue.

Some systems don't have DASH so a /bin/sh shebang (with no bashisms) ensures that the script will run in any distribution.

The BSDs have a "real" /bin/sh
https://www.freebsd.org/cgi/man.cgi?que … 9&sektion=

Offline

#12 2015-09-05 18:53:08

ostrołęk
CB Member
Registered: 2015-08-13
Posts: 110

Re: Unnecessarily using Bash instead of sh?

The Almquist shell (also known as A Shell, ash and sh) is a lightweight Unix shell originally written by Kenneth Almquist in the late 1980s. Initially a clone of the System V.4 variant of the Bourne shell, it replaced the original Bourne shell in the BSD versions of Unix released in the early 1990s. Derivative versions of ash are still installed as the default shell (/bin/sh) on FreeBSD, NetBSD, DragonFly BSD, MINIX, and Android, and in some Linux distributions.

Debian and derived Linux distributions such as Ubuntu ship a version of ash, known as dash (Debian Almquist shell), as the default /bin/sh, although Bash is the default login shell for interactive use. The reason for using dash is faster shell script execution, especially during startup of the operating system, compared to previous versions of Debian and Ubuntu that used Bash for this purpose. (Wikipedia)

In Debian/Ubuntu, /bin/sh is linked to /bin/dash in other Linux OSs to /bin/bash.

And, something about this so-called bashism...http://mywiki.wooledge.org/Bashism

Offline

#13 2015-09-06 08:26:38

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Unnecessarily using Bash instead of sh?

Speed: using sh can make a perceptible difference if it links to dash, as in Debian.
When first writing the "places" pipemenu for #! Statler, shifting from bash to dash, though it required calling sed instead of some nice Bash substitutions, still something like halved the execution time and removed the slight lag I was feeling. On modern hardware that might be less detectable though. ( It's useful to run 'time script.sh' while you're hacking on a script. )

Portability: while there is no such thing as the "sh" shell, there is the concept of POSIX compatibility. Dash satifies that spec (I think) while Bash, unless invoked with the --posix option, does not exactly.

Bash also supports a lot of other syntax, ie "bashisms". These make writing scripts much easier, but there's no guarantee those scripts will work in unusual situations. That's why a lot of "system" scripts use sh and keep things simple. Sometimes a system script has to use sh, as in the cron jobs that tknomanzr mentioned.

btw on Debian /bin and /usr/bin are two different directories with different content.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

#14 2015-09-06 12:11:12

ostrołęk
CB Member
Registered: 2015-08-13
Posts: 110

Re: Unnecessarily using Bash instead of sh?

^ You are right in a way, by using #! /bin/sh, it doesn't matter, whether that sh files links to in a Linux system. In Debian/Ubuntu it'd invoke dash, while in other systems, it'd invoke bash. Whatever it is, the speed is undetectable in new computers. Mine is more than 4 years old, your places-pipemenu works the same way in Debian, Ubuntu or Arch.  smile

Offline

#15 2015-09-07 20:53:06

tknomanzr
#! Die Hard
From: Heavener, OK
Registered: 2014-12-09
Posts: 777

Re: Unnecessarily using Bash instead of sh?

The more I thought about this,  the more I thought that wrapping a bash script in a sh compliant script is technically really bad form. However, given my admittedly limited shell scripting abilities plus the fact that I am not likely to have more than a few scripts cron scheduled, it works for me.

I think what would happen in this case, is that the shell script would spawn a separate bash process to run the script. While this is ok on my own home brewed machine, it would probably be exceptionally bad form (if it were even allowed at all) to do on my web server host's machines, for instance.

Offline

#16 2015-09-08 02:44:19

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Unnecessarily using Bash instead of sh?

BTW forgot to mention, one well-known place where bashisms cannot be used is openbox's autostart file, which is run by sh. From /usr/lib/i386-linux-gnu/openbox-autostart (the amd64 version should be the same):

AUTOSTART="${XDG_CONFIG_HOME:-"$HOME/.config"}/openbox/autostart"
# Run the user openbox autostart script
if test -f $AUTOSTART; then
    sh $AUTOSTART
elif test -f $AUTOSTART.sh; then
    sh $AUTOSTART.sh
fi

John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

#17 2015-09-08 04:43:08

ostrołęk
CB Member
Registered: 2015-08-13
Posts: 110

Re: Unnecessarily using Bash instead of sh?

^ What you haven't pasted,

GLOBALAUTOSTART="/etc/xdg/openbox/autostart"
AUTOSTART="${XDG_CONFIG_HOME:-"$HOME/.config"}/openbox/autostart"

shows what is AUTOSTART, and it doesn't point to any file with an extension. Linux is an extension-less system so a script doesn't necessarily have to have this characteristic in order to work.
It could be either AUTOSTART or AUTOSTART.sh

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo