How do I use command-line arguments to `sh` in the `-c` command string?
I know that
sh -c 'echo $1' sh 4
will output 4
.
and
sh -c 'echo $2' sh 4 5
will output 5
.
But I cannot understand how the parameters after the second sh
were passed to the command next to sh -c
. I read the man page of both bash
and dash
but could not find the introduction about this kind of syntax.
command-line bash sh dash-shell
add a comment |
I know that
sh -c 'echo $1' sh 4
will output 4
.
and
sh -c 'echo $2' sh 4 5
will output 5
.
But I cannot understand how the parameters after the second sh
were passed to the command next to sh -c
. I read the man page of both bash
and dash
but could not find the introduction about this kind of syntax.
command-line bash sh dash-shell
add a comment |
I know that
sh -c 'echo $1' sh 4
will output 4
.
and
sh -c 'echo $2' sh 4 5
will output 5
.
But I cannot understand how the parameters after the second sh
were passed to the command next to sh -c
. I read the man page of both bash
and dash
but could not find the introduction about this kind of syntax.
command-line bash sh dash-shell
I know that
sh -c 'echo $1' sh 4
will output 4
.
and
sh -c 'echo $2' sh 4 5
will output 5
.
But I cannot understand how the parameters after the second sh
were passed to the command next to sh -c
. I read the man page of both bash
and dash
but could not find the introduction about this kind of syntax.
command-line bash sh dash-shell
command-line bash sh dash-shell
edited yesterday
dessert
22.2k56198
22.2k56198
asked yesterday
gbcatgbcat
185
185
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
From man sh
:
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
In your command the second sh
is just a parameter with position 0
while 4
has position 1
and so on.
You can run this to check:
$ sh -c 'echo $0' sh 4 5
sh
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
– ilkkachu
yesterday
add a comment |
This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.
sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
command_string [command_name [argument...]]
See the command_string
parameter ? Now let's look at -c
flag description:
-c
Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.
In other words, where in normal shell script $0
(which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c
you have to specify it yourself. Thus,
sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5
would set the process name to sh
foobar.
Just in case you're wondering what $0
is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:
0
(Zero.) Expands to the name of the shell or shell script.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1107617%2fhow-do-i-use-command-line-arguments-to-sh-in-the-c-command-string%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
From man sh
:
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
In your command the second sh
is just a parameter with position 0
while 4
has position 1
and so on.
You can run this to check:
$ sh -c 'echo $0' sh 4 5
sh
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
– ilkkachu
yesterday
add a comment |
From man sh
:
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
In your command the second sh
is just a parameter with position 0
while 4
has position 1
and so on.
You can run this to check:
$ sh -c 'echo $0' sh 4 5
sh
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
– ilkkachu
yesterday
add a comment |
From man sh
:
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
In your command the second sh
is just a parameter with position 0
while 4
has position 1
and so on.
You can run this to check:
$ sh -c 'echo $0' sh 4 5
sh
From man sh
:
-c string If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
In your command the second sh
is just a parameter with position 0
while 4
has position 1
and so on.
You can run this to check:
$ sh -c 'echo $0' sh 4 5
sh
edited yesterday
dessert
22.2k56198
22.2k56198
answered yesterday
eyadofeyadof
1,20411517
1,20411517
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
– ilkkachu
yesterday
add a comment |
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
– ilkkachu
yesterday
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that
$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.– ilkkachu
yesterday
That seems to be the text in Bash's man page. It's actually incorrect in that it implies that
$0
is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.– ilkkachu
yesterday
add a comment |
This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.
sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
command_string [command_name [argument...]]
See the command_string
parameter ? Now let's look at -c
flag description:
-c
Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.
In other words, where in normal shell script $0
(which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c
you have to specify it yourself. Thus,
sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5
would set the process name to sh
foobar.
Just in case you're wondering what $0
is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:
0
(Zero.) Expands to the name of the shell or shell script.
add a comment |
This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.
sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
command_string [command_name [argument...]]
See the command_string
parameter ? Now let's look at -c
flag description:
-c
Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.
In other words, where in normal shell script $0
(which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c
you have to specify it yourself. Thus,
sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5
would set the process name to sh
foobar.
Just in case you're wondering what $0
is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:
0
(Zero.) Expands to the name of the shell or shell script.
add a comment |
This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.
sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
command_string [command_name [argument...]]
See the command_string
parameter ? Now let's look at -c
flag description:
-c
Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.
In other words, where in normal shell script $0
(which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c
you have to specify it yourself. Thus,
sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5
would set the process name to sh
foobar.
Just in case you're wondering what $0
is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:
0
(Zero.) Expands to the name of the shell or shell script.
This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.
sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
command_string [command_name [argument...]]
See the command_string
parameter ? Now let's look at -c
flag description:
-c
Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.
In other words, where in normal shell script $0
(which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c
you have to specify it yourself. Thus,
sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5
would set the process name to sh
foobar.
Just in case you're wondering what $0
is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:
0
(Zero.) Expands to the name of the shell or shell script.
edited yesterday
answered yesterday
Sergiy KolodyazhnyySergiy Kolodyazhnyy
70.1k9144307
70.1k9144307
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1107617%2fhow-do-i-use-command-line-arguments-to-sh-in-the-c-command-string%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