Shell variable value passing to subshell?Pipe stdout to another process's file descriptorCalling shell commands from RubyCheck if a directory exists in a shell scriptHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to use SSH to run a shell script on a remote machine?In the shell, what does “ 2>&1 ” mean?How to declare and use boolean variables in shell script?How to check if a variable is set in Bash?How to concatenate string variables in BashHow to set a variable to the output of a command in Bash?Check existence of input argument in a Bash shell script

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Why did the EU agree to delay the Brexit deadline?

What should you do when eye contact makes your subordinate uncomfortable?

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

What are the advantages of simplicial model categories over non-simplicial ones?

Yosemite Fire Rings - What to Expect?

Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?

Why is so much work done on numerical verification of the Riemann Hypothesis?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

What is Cash Advance APR?

The probability of Bus A arriving before Bus B

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Temporarily disable WLAN internet access for children, but allow it for adults

How much character growth crosses the line into breaking the character

What exact color does ozone gas have?

The IT department bottlenecks progress. How should I handle this?

Can I visit Japan without a visa?

A social experiment. What is the worst that can happen?

Limits and Infinite Integration by Parts

Why Shazam when there is already Superman?

Can I still be respawned if I die by falling off the map?

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

Strong empirical falsification of quantum mechanics based on vacuum energy density



Shell variable value passing to subshell?


Pipe stdout to another process's file descriptorCalling shell commands from RubyCheck if a directory exists in a shell scriptHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to use SSH to run a shell script on a remote machine?In the shell, what does “ 2>&1 ” mean?How to declare and use boolean variables in shell script?How to check if a variable is set in Bash?How to concatenate string variables in BashHow to set a variable to the output of a command in Bash?Check existence of input argument in a Bash shell script













-1















I am writing script in shell and it's something like:



temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"


( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?










share|improve this question









New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    In while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp", the $temp on the right hand side of the pipe is evaluated before the while loop is executed.

    – William Pursell
    yesterday






  • 1





    Please show what you're actually trying to do in the right side of the pipe. Piping to echo doesn't make sense since echo doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.

    – John Kugelman
    yesterday















-1















I am writing script in shell and it's something like:



temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"


( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?










share|improve this question









New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    In while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp", the $temp on the right hand side of the pipe is evaluated before the while loop is executed.

    – William Pursell
    yesterday






  • 1





    Please show what you're actually trying to do in the right side of the pipe. Piping to echo doesn't make sense since echo doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.

    – John Kugelman
    yesterday













-1












-1








-1








I am writing script in shell and it's something like:



temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"


( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?










share|improve this question









New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am writing script in shell and it's something like:



temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"


( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?







shell






share|improve this question









New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday







Darman41













New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Darman41Darman41

11




11




New contributor




Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Darman41 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    In while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp", the $temp on the right hand side of the pipe is evaluated before the while loop is executed.

    – William Pursell
    yesterday






  • 1





    Please show what you're actually trying to do in the right side of the pipe. Piping to echo doesn't make sense since echo doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.

    – John Kugelman
    yesterday












  • 1





    In while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp", the $temp on the right hand side of the pipe is evaluated before the while loop is executed.

    – William Pursell
    yesterday






  • 1





    Please show what you're actually trying to do in the right side of the pipe. Piping to echo doesn't make sense since echo doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.

    – John Kugelman
    yesterday







1




1





In while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp", the $temp on the right hand side of the pipe is evaluated before the while loop is executed.

– William Pursell
yesterday





In while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp", the $temp on the right hand side of the pipe is evaluated before the while loop is executed.

– William Pursell
yesterday




1




1





Please show what you're actually trying to do in the right side of the pipe. Piping to echo doesn't make sense since echo doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.

– John Kugelman
yesterday





Please show what you're actually trying to do in the right side of the pipe. Piping to echo doesn't make sense since echo doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.

– John Kugelman
yesterday












1 Answer
1






active

oldest

votes


















0














Do you want it like this? Prints 5 and 0.



#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat

echo "temp unchanged because of subshell $temp"


Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.



#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done

if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi


temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter

echo "temp unchanged because of subshell $temp"





share|improve this answer










New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

    – Darman41
    yesterday











  • stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

    – xenoson
    yesterday











  • I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

    – Darman41
    yesterday











  • Maybe use another file descriptor.

    – xenoson
    yesterday












  • sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

    – Darman41
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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
);



);






Darman41 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55280880%2fshell-variable-value-passing-to-subshell%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









0














Do you want it like this? Prints 5 and 0.



#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat

echo "temp unchanged because of subshell $temp"


Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.



#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done

if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi


temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter

echo "temp unchanged because of subshell $temp"





share|improve this answer










New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

    – Darman41
    yesterday











  • stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

    – xenoson
    yesterday











  • I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

    – Darman41
    yesterday











  • Maybe use another file descriptor.

    – xenoson
    yesterday












  • sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

    – Darman41
    yesterday















0














Do you want it like this? Prints 5 and 0.



#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat

echo "temp unchanged because of subshell $temp"


Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.



#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done

if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi


temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter

echo "temp unchanged because of subshell $temp"





share|improve this answer










New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

    – Darman41
    yesterday











  • stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

    – xenoson
    yesterday











  • I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

    – Darman41
    yesterday











  • Maybe use another file descriptor.

    – xenoson
    yesterday












  • sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

    – Darman41
    yesterday













0












0








0







Do you want it like this? Prints 5 and 0.



#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat

echo "temp unchanged because of subshell $temp"


Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.



#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done

if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi


temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter

echo "temp unchanged because of subshell $temp"





share|improve this answer










New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










Do you want it like this? Prints 5 and 0.



#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat

echo "temp unchanged because of subshell $temp"


Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.



#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done

if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi


temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter

echo "temp unchanged because of subshell $temp"






share|improve this answer










New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited yesterday





















New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered yesterday









xenosonxenoson

533




533




New contributor




xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






xenoson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

    – Darman41
    yesterday











  • stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

    – xenoson
    yesterday











  • I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

    – Darman41
    yesterday











  • Maybe use another file descriptor.

    – xenoson
    yesterday












  • sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

    – Darman41
    yesterday

















  • Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

    – Darman41
    yesterday











  • stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

    – xenoson
    yesterday











  • I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

    – Darman41
    yesterday











  • Maybe use another file descriptor.

    – xenoson
    yesterday












  • sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

    – Darman41
    yesterday
















Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

– Darman41
yesterday





Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell

– Darman41
yesterday













stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

– xenoson
yesterday





stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?

– xenoson
yesterday













I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

– Darman41
yesterday





I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines

– Darman41
yesterday













Maybe use another file descriptor.

– xenoson
yesterday






Maybe use another file descriptor.

– xenoson
yesterday














sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

– Darman41
yesterday





sadly I don't have those 20 lines in file and I am not allowed to create temporarily file

– Darman41
yesterday












Darman41 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Darman41 is a new contributor. Be nice, and check out our Code of Conduct.












Darman41 is a new contributor. Be nice, and check out our Code of Conduct.











Darman41 is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55280880%2fshell-variable-value-passing-to-subshell%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript