0

I see this in a lot of installation instructions for certain libraries, where you're given some installation.sh file, and the instructions say to

chmod + x installation.sh
./installation.sh

I am wondering what is the point of this, when you can just do

sh installation.sh

I'm not well versed with shell scripting.

roulette01
  • 1,984
  • 2
  • 13
  • 26
  • 2
    This question would be better placed at [unix.se] or [Super User](https://superuser.com); Stack Overflow is only for questions about specific technical problems you actually face encountered in the course of software development. – Charles Duffy Apr 18 '22 at 14:43
  • 2
    That said, the main advantage is that when your file is invoked as an executable, its shebang is honored. Not all `.sh` files are really meant to be run with `sh` -- a script that starts with `#!/usr/bin/env bash`, for example, is supposed to be run with `bash`, not `sh`, and may use language features `sh` doesn't provide. – Charles Duffy Apr 18 '22 at 14:43
  • 2
    (this is part of why giving scripts `.sh` extensions is a bad idea in the first place: that extension doesn't reliably mean anything useful in practice. See also the essay [Commandname Extensions Considered Harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/), which has been referenced from the #bash IRC channel's factoid database for well over a decade now). – Charles Duffy Apr 18 '22 at 14:44
  • Also see [this question](https://stackoverflow.com/q/15179446/1899640) for the problems you can run into when running `sh installation.sh` – that other guy Apr 18 '22 at 16:11
  • There should be no space between `+` and `x` in your `chmod` command. – tripleee Apr 19 '22 at 08:11
  • Please consider [accepting](https://stackoverflow.com/help/someone-answers) the answer, if it matches your question. If it does not, please state what else is missing. – user1934428 Aug 31 '23 at 07:09

1 Answers1

2

Running it with sh installtion.sh guarantees you that the script is processed with sh (i.e. you are responsible to know which language the script is written in), and installation.sh must be in your working directory.

Running it with installation.sh (which is only possible if the script is executable) guarantees that the script is processed with that language processor (sh, zsh, python,....) which is defined in the #! line of the script. In addition, installation.sh must be in some directory of your PATH.

It's up to you what you prefer; but if you do a chmod +x, the user of the script can choose whether he is using the first or second variant. If you don't chmod it, the user of the script can only do the first variant.

A (perhaps small) side effect of setting the x-bit is that some tools (for example ls) can be configured to colour executable files differently from non-executable ones, which may be helpful if you do a ls of a bunch of files.

user1934428
  • 19,864
  • 7
  • 42
  • 87