13

From Google's Shell Style Guide:

Bash is the only shell scripting language permitted for executables.

Executables must start with #!/bin/bash and a minimum number of flags. Use set to set shell options so that calling your script as bash script_name does not break its functionality.

Specifically, the part about using set to avoid breaking functionality. And what does calling it in that particular way have to do with it?

2
  • 1
    I believe this Google guide strictly applies only to scripts hosted in their Cloud service. It has some reasonable advice, but is excessively over-prescriptive in places. Commented Oct 17, 2020 at 20:47
  • 3
    @Paul_Pedant the style guide isn’t prescriptive for Google’s cloud services, it applies to code developed by Google. See the general description for context. Commented Oct 17, 2020 at 21:26

1 Answer 1

30

The "and a minimum number of flags" refers to flags set in the hashbang line. They'd be read when the script is started as ./somescript, and the kernel reads the hashbang line, building a new argument list from the path and options found there. But this does not happen if the script is started as bash somescript, as the kernel is asked to run bash, and not the script itself. The shell itself sees the line as a comment to be ignored when eventually reading the script.

For example, try the following script (in ./hello):

#!/bin/bash -x
echo hello

And run it in both of the two ways:

$ ./hello
+ echo hello
hello
$ bash hello
hello

The extra trace output from -x is shown only in the first case, the flag is ignored with the second invocation.

Enabling the flag explicitly with a set command would make it work the same either way:

#!/bin/bash
set -x
echo hello

(though note that if you ever try this with perl, it does interpret the hashbang line itself, too.)

8
  • 3
    from the same guide (and some Unix guide of old) "Executables should have no extension". Using a file extension to mark the language, breaks encapsulation. Commented Oct 17, 2020 at 21:24
  • 4
    @ctrl-alt-delor, hah, well, yes. But source code files usually do have an extension (be it .c or .pl or .py), so maybe that's where the habit has come. Also some editors look at the extension to guess the language...
    – ilkkachu
    Commented Oct 18, 2020 at 8:23
  • 1
    @typo, maybe they want to save typing the three letters from set... Perhaps some might also not know that it's possible to use set -x instead. Who knows. In some cases you have to do it, though. E.g. with awk and expect the hashbang needs to be #!/usr/bin/expect -f, because they need the -f option to point to a file on the command line. And in Perl, there are some settings that can only be set there, and not after the interpreter actually initializes (-T for taint checking at least). But I don't think there's any such reason with the shell.
    – ilkkachu
    Commented Oct 19, 2020 at 7:56
  • 1
    @ilkkachu: I believe that the -p (lower-case P = “privileged” mode) flag must be specified on the invocation of the shell (i.e., on the hashbang) in order to be effective.  Clearly this is true for the --norc option as well, and maybe also --login, --rcfile file and --posix. Commented Oct 19, 2020 at 8:57
  • 2
    @G-ManSays'ReinstateMonica', yep, I should have written "I can't think of any". There's set -p, which drops extra privileges, but probably doesn't undo importing functions (bash -p prevents the import). --login or -l probably has no equivalent, at least it's listed in the man page separate from set options. And it affects startup files too, like --rcfile, and there's no way to do that after startup. There's set -o posix, which probably does the same as --posix, except if there was some difference to startup files here, too.
    – ilkkachu
    Commented Oct 19, 2020 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .