0

I recently started to develop my Linux POSIX shell scripts in a more structured way.


Let me explain:

Code [A] is being sourced by some code [B], just some minimal example:

#!/bin/sh

# REQUIREMENTS
# none

# METHODS (public)
# none; this file when sourced sets up basic tput colors, if available
...

Code [B] is being executed, again some minimal example follows:

#!/bin/sh

# REQUIREMENTS
. /home/username/Development/sh/functions/func-color_support

# METHODS (public)
print_error ()
# this prints custom heading and a given error message
{
...
}

What I did not originally realize, because it works fine in recent versions of Bash and Dash, is that I am duplicating the shebang (#!/bin/sh) each time I source some of the function file(s). I am working in VS Code with self-compiled ShellCheck and the obvious reason for me to add the shebang is naturally wanting to edit and update the functions without having to change the syntax highlighter every time.


Hence my question follows:

Is duplication of the shebang (#!/bin/sh) violating any POSIX shell programming rules or guidelines? Further, can duplication of POSIX shebang (#!/bin/sh) when sourcing files into one piece cause problems, be it practical or in theory?


I posted this on StackOverflow yesterday and still not having an answer, but this comment seems promising:

  • See 2.1.1 here, quote:

If the first line of a file of shell commands starts with the characters "#!", the results are unspecified.

1

2 Answers 2

2

Is duplication of the shebang (#!/bin/sh) violating any POSIX shell programming rules or guidelines?

I don't believe so, no. Look at the definition for dot on the same page:

The shell shall execute commands from the file in the current environment.

The first line from a sourced (dot) file is not the first line of a file of shell commands because we're just adding them into the current environment. Therefore I think that your concern that "If the first line of a file of shell commands starts with the characters "#!", the results are unspecified." does not apply.

Further, can duplication of POSIX shebang (#!/bin/sh) when sourcing files into one piece cause problems, be it practical or in theory?

I myself have never seen such problems. In this context the # is just a comment marker.

2

Not a problem. The shebang line (#!/bin/bash) looks like a #-comment to bash.

The #! is only interpreted when you start the script.

1

You must log in to answer this question.

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