How does this shebang that starts with a double hyphen (--) work?
up vote
4
down vote
favorite
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 #! ?
https://rosettacode.org/wiki/Multiline_shebang#PostgreSQL
shell-script scripting sql shebang db2
add a comment |
up vote
4
down vote
favorite
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 #! ?
https://rosettacode.org/wiki/Multiline_shebang#PostgreSQL
shell-script scripting sql shebang db2
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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 #! ?
https://rosettacode.org/wiki/Multiline_shebang#PostgreSQL
shell-script scripting sql shebang db2
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 #! ?
https://rosettacode.org/wiki/Multiline_shebang#PostgreSQL
shell-script scripting sql shebang db2
shell-script scripting sql shebang db2
edited 18 mins ago
muru
35.4k582157
35.4k582157
asked 4 hours ago
AngocA
1458
1458
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
Related: Which shell interpreter runs a script with no shebang?
The script does not have a shebang line. A double dash is no shebang.
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:
-- () {
:
}
The purpose of the function definition is to provide a way of inserting a ;
and another command on the same line. After declaring the bogus shell function, the script 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 script pathname from the command line.
This trick would probably not work reliably on systems where dash
is used as /bin/sh
, as that shell does not accept functions whose names contain dashes.
Also related:
- Shell valid function name characters
- Will it be bad that a function or script name contains dash `-` instead of underline `_`?
I suppose the following would also work (not really tested):
--() { exec db2 -txf "$0"; }; --
add a comment |
up vote
0
down vote
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
.
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f488068%2fhow-does-this-shebang-that-starts-with-a-double-hyphen-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Related: Which shell interpreter runs a script with no shebang?
The script does not have a shebang line. A double dash is no shebang.
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:
-- () {
:
}
The purpose of the function definition is to provide a way of inserting a ;
and another command on the same line. After declaring the bogus shell function, the script 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 script pathname from the command line.
This trick would probably not work reliably on systems where dash
is used as /bin/sh
, as that shell does not accept functions whose names contain dashes.
Also related:
- Shell valid function name characters
- Will it be bad that a function or script name contains dash `-` instead of underline `_`?
I suppose the following would also work (not really tested):
--() { exec db2 -txf "$0"; }; --
add a comment |
up vote
5
down vote
Related: Which shell interpreter runs a script with no shebang?
The script does not have a shebang line. A double dash is no shebang.
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:
-- () {
:
}
The purpose of the function definition is to provide a way of inserting a ;
and another command on the same line. After declaring the bogus shell function, the script 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 script pathname from the command line.
This trick would probably not work reliably on systems where dash
is used as /bin/sh
, as that shell does not accept functions whose names contain dashes.
Also related:
- Shell valid function name characters
- Will it be bad that a function or script name contains dash `-` instead of underline `_`?
I suppose the following would also work (not really tested):
--() { exec db2 -txf "$0"; }; --
add a comment |
up vote
5
down vote
up vote
5
down vote
Related: Which shell interpreter runs a script with no shebang?
The script does not have a shebang line. A double dash is no shebang.
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:
-- () {
:
}
The purpose of the function definition is to provide a way of inserting a ;
and another command on the same line. After declaring the bogus shell function, the script 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 script pathname from the command line.
This trick would probably not work reliably on systems where dash
is used as /bin/sh
, as that shell does not accept functions whose names contain dashes.
Also related:
- Shell valid function name characters
- Will it be bad that a function or script name contains dash `-` instead of underline `_`?
I suppose the following would also work (not really tested):
--() { exec db2 -txf "$0"; }; --
Related: Which shell interpreter runs a script with no shebang?
The script does not have a shebang line. A double dash is no shebang.
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:
-- () {
:
}
The purpose of the function definition is to provide a way of inserting a ;
and another command on the same line. After declaring the bogus shell function, the script 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 script pathname from the command line.
This trick would probably not work reliably on systems where dash
is used as /bin/sh
, as that shell does not accept functions whose names contain dashes.
Also related:
- Shell valid function name characters
- Will it be bad that a function or script name contains dash `-` instead of underline `_`?
I suppose the following would also work (not really tested):
--() { exec db2 -txf "$0"; }; --
edited 3 hours ago
answered 4 hours ago
Kusalananda
120k16225367
120k16225367
add a comment |
add a comment |
up vote
0
down vote
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
.
New contributor
add a comment |
up vote
0
down vote
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
.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
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
.
New contributor
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
.
New contributor
edited 3 hours ago
New contributor
answered 3 hours ago
Uncle Billy
462
462
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f488068%2fhow-does-this-shebang-that-starts-with-a-double-hyphen-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown