Process substitution inside a subshell to set a variable
I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.
Here's the pattern I'm trying:
var=$(bash <(curl -fsSkL http://remote/file.sh))
echo "var=${var}"
I'm testing this pattern without curl
using cat
:
var=$(bash <(cat ./local/file.sh))
echo "var=${var}"
This should be the same as far as syntax is concerned. ./local/file.sh
contains echo hello
, so I would expect var
to contain the value hello
, but alas, executing the above results in the following:
test.sh: command substitution: line 4: syntax error near unexpected token `('
test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
var=
How can I accomplish my goal without using temporary files?
bash variable subshell process-substitution
add a comment |
I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.
Here's the pattern I'm trying:
var=$(bash <(curl -fsSkL http://remote/file.sh))
echo "var=${var}"
I'm testing this pattern without curl
using cat
:
var=$(bash <(cat ./local/file.sh))
echo "var=${var}"
This should be the same as far as syntax is concerned. ./local/file.sh
contains echo hello
, so I would expect var
to contain the value hello
, but alas, executing the above results in the following:
test.sh: command substitution: line 4: syntax error near unexpected token `('
test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
var=
How can I accomplish my goal without using temporary files?
bash variable subshell process-substitution
add a comment |
I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.
Here's the pattern I'm trying:
var=$(bash <(curl -fsSkL http://remote/file.sh))
echo "var=${var}"
I'm testing this pattern without curl
using cat
:
var=$(bash <(cat ./local/file.sh))
echo "var=${var}"
This should be the same as far as syntax is concerned. ./local/file.sh
contains echo hello
, so I would expect var
to contain the value hello
, but alas, executing the above results in the following:
test.sh: command substitution: line 4: syntax error near unexpected token `('
test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
var=
How can I accomplish my goal without using temporary files?
bash variable subshell process-substitution
I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files.
Here's the pattern I'm trying:
var=$(bash <(curl -fsSkL http://remote/file.sh))
echo "var=${var}"
I'm testing this pattern without curl
using cat
:
var=$(bash <(cat ./local/file.sh))
echo "var=${var}"
This should be the same as far as syntax is concerned. ./local/file.sh
contains echo hello
, so I would expect var
to contain the value hello
, but alas, executing the above results in the following:
test.sh: command substitution: line 4: syntax error near unexpected token `('
test.sh: command substitution: line 4: `bash <(cat ./local/file.sh)'
var=
How can I accomplish my goal without using temporary files?
bash variable subshell process-substitution
bash variable subshell process-substitution
asked 2 hours ago
Sean AllredSean Allred
8143816
8143816
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Those are the errors you get when trying to perform a process substitution in bash
when the shell is running in POSIX mode. The bash
shell does not support process substitutions in POSIX mode.
bash
will run in POSIX mode when either
set -o posix
has been used, or- the shell is being invoked as
sh
.
My hunch is that you have a script, test.sh
, that you are running with sh test.sh
or that has a #!/bin/sh
hashbang line, and that your sh
happens to be bash
. Another possibility is that the script does not have #!
-line at all, and it is being invoked by bash
-as-sh
in some other way.
Instead, see to that your test.sh
script is being invoked by bash
.
Example:
$ cat script.sh
echo hello
$ cat test.sh
var=$(bash <( cat script.sh ))
printf 'var="%s"n' "$var"
$ bash -o posix test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `bash <( cat script.sh ))'
var=""
$ bash test.sh
var="hello"
Yep, I was invoking assh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensurebash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse ofset -o posix
?
– Sean Allred
2 hours ago
@SeanAllred Sincesh
does not need to bebash
and indeed,sh
is often notbash
, you should rather make sure that your scripts are executed bybash
. A simple way to do this is to make the script executable and add#!/bin/bash
as the first line in the script (adapt that to the correct path to thebash
executable on your system). Then run it without specifying an explicit interpreter.
– Kusalananda
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to optionposix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)
– Sean Allred
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a#!/bin/bash
line! Thesh
shell on many Linux machines isdash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD),sh
ispdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.
– Kusalananda
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
|
show 1 more 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',
autoActivateHeartbeat: false,
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%2f502595%2fprocess-substitution-inside-a-subshell-to-set-a-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Those are the errors you get when trying to perform a process substitution in bash
when the shell is running in POSIX mode. The bash
shell does not support process substitutions in POSIX mode.
bash
will run in POSIX mode when either
set -o posix
has been used, or- the shell is being invoked as
sh
.
My hunch is that you have a script, test.sh
, that you are running with sh test.sh
or that has a #!/bin/sh
hashbang line, and that your sh
happens to be bash
. Another possibility is that the script does not have #!
-line at all, and it is being invoked by bash
-as-sh
in some other way.
Instead, see to that your test.sh
script is being invoked by bash
.
Example:
$ cat script.sh
echo hello
$ cat test.sh
var=$(bash <( cat script.sh ))
printf 'var="%s"n' "$var"
$ bash -o posix test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `bash <( cat script.sh ))'
var=""
$ bash test.sh
var="hello"
Yep, I was invoking assh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensurebash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse ofset -o posix
?
– Sean Allred
2 hours ago
@SeanAllred Sincesh
does not need to bebash
and indeed,sh
is often notbash
, you should rather make sure that your scripts are executed bybash
. A simple way to do this is to make the script executable and add#!/bin/bash
as the first line in the script (adapt that to the correct path to thebash
executable on your system). Then run it without specifying an explicit interpreter.
– Kusalananda
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to optionposix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)
– Sean Allred
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a#!/bin/bash
line! Thesh
shell on many Linux machines isdash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD),sh
ispdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.
– Kusalananda
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
|
show 1 more comment
Those are the errors you get when trying to perform a process substitution in bash
when the shell is running in POSIX mode. The bash
shell does not support process substitutions in POSIX mode.
bash
will run in POSIX mode when either
set -o posix
has been used, or- the shell is being invoked as
sh
.
My hunch is that you have a script, test.sh
, that you are running with sh test.sh
or that has a #!/bin/sh
hashbang line, and that your sh
happens to be bash
. Another possibility is that the script does not have #!
-line at all, and it is being invoked by bash
-as-sh
in some other way.
Instead, see to that your test.sh
script is being invoked by bash
.
Example:
$ cat script.sh
echo hello
$ cat test.sh
var=$(bash <( cat script.sh ))
printf 'var="%s"n' "$var"
$ bash -o posix test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `bash <( cat script.sh ))'
var=""
$ bash test.sh
var="hello"
Yep, I was invoking assh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensurebash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse ofset -o posix
?
– Sean Allred
2 hours ago
@SeanAllred Sincesh
does not need to bebash
and indeed,sh
is often notbash
, you should rather make sure that your scripts are executed bybash
. A simple way to do this is to make the script executable and add#!/bin/bash
as the first line in the script (adapt that to the correct path to thebash
executable on your system). Then run it without specifying an explicit interpreter.
– Kusalananda
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to optionposix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)
– Sean Allred
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a#!/bin/bash
line! Thesh
shell on many Linux machines isdash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD),sh
ispdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.
– Kusalananda
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
|
show 1 more comment
Those are the errors you get when trying to perform a process substitution in bash
when the shell is running in POSIX mode. The bash
shell does not support process substitutions in POSIX mode.
bash
will run in POSIX mode when either
set -o posix
has been used, or- the shell is being invoked as
sh
.
My hunch is that you have a script, test.sh
, that you are running with sh test.sh
or that has a #!/bin/sh
hashbang line, and that your sh
happens to be bash
. Another possibility is that the script does not have #!
-line at all, and it is being invoked by bash
-as-sh
in some other way.
Instead, see to that your test.sh
script is being invoked by bash
.
Example:
$ cat script.sh
echo hello
$ cat test.sh
var=$(bash <( cat script.sh ))
printf 'var="%s"n' "$var"
$ bash -o posix test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `bash <( cat script.sh ))'
var=""
$ bash test.sh
var="hello"
Those are the errors you get when trying to perform a process substitution in bash
when the shell is running in POSIX mode. The bash
shell does not support process substitutions in POSIX mode.
bash
will run in POSIX mode when either
set -o posix
has been used, or- the shell is being invoked as
sh
.
My hunch is that you have a script, test.sh
, that you are running with sh test.sh
or that has a #!/bin/sh
hashbang line, and that your sh
happens to be bash
. Another possibility is that the script does not have #!
-line at all, and it is being invoked by bash
-as-sh
in some other way.
Instead, see to that your test.sh
script is being invoked by bash
.
Example:
$ cat script.sh
echo hello
$ cat test.sh
var=$(bash <( cat script.sh ))
printf 'var="%s"n' "$var"
$ bash -o posix test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `bash <( cat script.sh ))'
var=""
$ bash test.sh
var="hello"
edited 2 hours ago
answered 2 hours ago
KusalanandaKusalananda
132k17250411
132k17250411
Yep, I was invoking assh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensurebash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse ofset -o posix
?
– Sean Allred
2 hours ago
@SeanAllred Sincesh
does not need to bebash
and indeed,sh
is often notbash
, you should rather make sure that your scripts are executed bybash
. A simple way to do this is to make the script executable and add#!/bin/bash
as the first line in the script (adapt that to the correct path to thebash
executable on your system). Then run it without specifying an explicit interpreter.
– Kusalananda
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to optionposix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)
– Sean Allred
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a#!/bin/bash
line! Thesh
shell on many Linux machines isdash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD),sh
ispdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.
– Kusalananda
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
|
show 1 more comment
Yep, I was invoking assh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensurebash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse ofset -o posix
?
– Sean Allred
2 hours ago
@SeanAllred Sincesh
does not need to bebash
and indeed,sh
is often notbash
, you should rather make sure that your scripts are executed bybash
. A simple way to do this is to make the script executable and add#!/bin/bash
as the first line in the script (adapt that to the correct path to thebash
executable on your system). Then run it without specifying an explicit interpreter.
– Kusalananda
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to optionposix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)
– Sean Allred
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a#!/bin/bash
line! Thesh
shell on many Linux machines isdash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD),sh
ispdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.
– Kusalananda
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
Yep, I was invoking as
sh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix
?– Sean Allred
2 hours ago
Yep, I was invoking as
sh
– I didn't realize POSIX didn't support these features. Thanks! As a short follow-up, is there a way to ensure bash
-as-sh
(if invoked incorrectly) will not run in POSIX mode? In other words, what is the converse of set -o posix
?– Sean Allred
2 hours ago
@SeanAllred Since
sh
does not need to be bash
and indeed, sh
is often not bash
, you should rather make sure that your scripts are executed by bash
. A simple way to do this is to make the script executable and add #!/bin/bash
as the first line in the script (adapt that to the correct path to the bash
executable on your system). Then run it without specifying an explicit interpreter.– Kusalananda
2 hours ago
@SeanAllred Since
sh
does not need to be bash
and indeed, sh
is often not bash
, you should rather make sure that your scripts are executed by bash
. A simple way to do this is to make the script executable and add #!/bin/bash
as the first line in the script (adapt that to the correct path to the bash
executable on your system). Then run it without specifying an explicit interpreter.– Kusalananda
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option
posix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)– Sean Allred
2 hours ago
Yes I can ensure that my tools use bash, via shebang or otherwise, but I'm concerned that those using the script I'm making will not invoke it correctly. Is there no converse to option
posix
? (Working under the assumption now that there's no means by which to accomplish my goal – without temp files – with only POSIX features.)– Sean Allred
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a
#!/bin/bash
line! The sh
shell on many Linux machines is dash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh
is pdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.– Kusalananda
2 hours ago
@SeanAllred If you are sharing your script with others, it extra important to have a
#!/bin/bash
line! The sh
shell on many Linux machines is dash
, and that shell would not understand process substitutions however hard you tried. On my machine (OpenBSD), sh
is pdksh
, and that also can't be made to run process substitutions. What you are asking is the equivalent to how to make someone invoke a Python script with the correct Python interpreter even when they run it with a Ruby interpreter. If the user picks the wrong interpreter, then the documentation was not good enough.– Kusalananda
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
Well of course there will be a shebang line :-) but fair enough – I guess there's a limit to how much you can protect others from themselves. Thanks for your help!
– Sean Allred
2 hours ago
|
show 1 more 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.
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%2f502595%2fprocess-substitution-inside-a-subshell-to-set-a-variable%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