I have found the following kind of shebang in the RosettaCode page:

--() { :; }; exec db2 -txf "$0"

It works for Db2, and a similar thing for Postgres. However, I do not understand the whole line.

I know the double dash is a comment in SQL, and after that it calls the Db2 executable with some parameters passing the file itself as file. But what about the parenthesis, the curly brakets, the colon and semi-colon, and how can replace a real shebang #! ?


2 Answers 2


Related: Which shell interpreter runs a script with no shebang?

The script does not have a shebang/hashbang/#! line, simply because a double dash is not #!.

However, the script will be executed by a shell (see above linked question and answers), and in that shell, if - is a valid character in a function name, the line declares a shell function called -- that does nothing (well, it runs :, which does nothing) and which is never called.

The function, in the more common multi-line notation (just to make it more obvious what it looks like, as its odd name kinda obscures the fact that it's in fact a function):

-- () {

The sole purpose of the function definition is to have a line that is valid in a shell script and at the same time a valid SQL command (a comment). This sort of code is called a polyglot.

After declaring the bogus shell function, the script, when executed by a shell script interpreter, uses exec to replace the current shell with the process resulting from running db2 -txf "$0", which would be the same as using db2 -txf on the pathname of the script from the command line.

This trick would probably not work reliably on systems where dash or other ash-based shells, yash, the Bourne shell, ksh88 or ksh93 is used as /bin/sh, as these shell do not accept functions whose name contains dashes.

Also related:

I suppose the following would also work (not really tested):

--() { exec db2 -txf "$0"; }; --

As @Kusalananda has already said, that trick is broken and it won't work in all shells.

Here is my take at doing it portably:

--/.. 2>/dev/null; exec db2 -txf "$0"

The first command should fail even if a file/directory named -- exists in the current directory and any errors will be shut up by the 2>/dev/null; the shell will then proceed with the second command, the exec.

  • It's still not really portable. It's not a valid script and you are still relying on the calling shell to work around the fact that the kernel will refuse to run the script and returns ENOEXEC if you try to. Try running the script under strace to see what I mean.
    – kasperd
    Dec 15, 2018 at 13:40
  • @kasperd, it should still be portable, the shell is supposed to run the script as a shell script if exec() doesn't work on it. "If the execl() function fails due to an error equivalent to the [ENOEXEC] error, the shell shall execute a command equivalent to having a shell invoked with the command name as its first operand, ... " (see pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…)
    – ilkkachu
    Dec 15, 2018 at 14:13
  • @ilkkachu But scripts are not always executed from a shell. If you try to use the script in any other context where an executable would work it's going to fail. Moreover shells don't agree on which interpreter to use. So your script will now behave differently or fail altogether depending on which context it is being called from.
    – kasperd
    Dec 15, 2018 at 14:28
  • @kasperd, well, sure, it won't work if you exec() it directly from something other than a shell. But what would that case be? You might want to run the script from cron or such, but I think it runs everything through a shell anyway, and even if not, it's easy to just spell out db2 -txf /path/to/script in that case, since you only need to do it once. Having the shorthand work is mostly useful on an interactive shell. But sure, a separate wrapper script might be more robust.
    – ilkkachu
    Dec 15, 2018 at 15:01
  • 1
    @kasperd I won't annoy you with docs and standards; just try it! echo 'int main(int c,char**a){execvp(a[1],a+1);}' | cc -include unistd.h -xc -; echo echo yeah > a.sh; chmod 755 a.sh; ./a.out ./a.sh; PATH=`pwd` ./a.out a.sh
    – user313992
    Dec 16, 2018 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.