Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:39:10+00:00 2026-05-23T00:39:10+00:00

unset v function f { v=1 } f | cat echo v=$v f echo

  • 0
unset v
function f {
  v=1
}
f | cat
echo v=$v
f 
echo v=$v

Why does piping (to any command) prevent the first echo command from printing 1? The second echo prints 1. I’m using a bash shell. I can see this by copy/paste or by running this as a script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:39:10+00:00Added an answer on May 23, 2026 at 12:39 am

    All components of a pipeline (if more than one) are executed in a subshell, and their variable assignments do not persist to the main shell.

    The reason for this is that bash does not support real multithreading (with concurrent access to variables), only subprocesses which run in parallel.


    How to avoid this:

    You have to do any variable assignments you want to retain in the main bash process (or find some way to transfer them there). The bash way of doing this would be not to use a pipe, but use process substitution instead:

    f > >( cat )
    

    Of course, this will not help if you need to do variable assignments in both processes of a pipe. Then you have to think of a better mechanism instead (maybe coprocesses, and output the variables somewhere?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function get_arr($arr) { unset($arr[0]); } $arr1 = array(1,2); $arr2 = array(1,2); get_arr(&$arr1); get_arr($arr2); echo
I've found a strange issue when using unset function in combination with a SimpleXML
I am trying to unset function using be searching for all of them and
How can I unset variable? For example, PHP has an unset($var) function.
How do i unset a already set flag using fcntl? For e.g. I can
I have a key that appears to be an empty string, however using unset($array[]);
Firefox 3 has introduced a new behavior in which line-height, when unset, differs from
$test = 'aaaaaa'; $abc = & $test; unset($test); echo $abc; It outputs 'aaaaaa',which is
I'm accessing an array by reference inside a foreach loop, but the unset() function
I made this simple function (remove all $elem from $array): function remall($array, $elem) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.