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 8208777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:29:30+00:00 2026-06-07T09:29:30+00:00

This is an attempt to simplify a problem I’m having. I define a function

  • 0

This is an attempt to simplify a problem I’m having.
I define a function which sets a variable and this works in this scenario:

$ function myfunc { res="ABC" ; }
$ res="XYZ"
$ myfunc
$ echo $res
    ABC

So res has been changed by the call to myfunc. But:

$ res="XYZ"
$ myfunc | echo
$ echo $res
    XYZ

So when myfunc is part of a pipe the value doesn’t change.
How can I make myfunc work the way I desire even when a pipe is involved?

(In the real script “myfunc” does something more elaborate of course and the other side of the pipe has a zenity progress dialogue rather than a pointless echo)

Thanks

  • 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-06-07T09:29:31+00:00Added an answer on June 7, 2026 at 9:29 am

    This isn’t possible on Unix. To understand this better, you need to know what a variable is. Bash keeps two internal tables with all defined variables. One is for variables local to the current shell. You can create those with set name=value or just name=value. These are local to the process; they are not inherited when a new process is created.

    To export a variable to new child processes, you must export it with export name. That tells bash “I want children to see the value of this variable”. It’s a security feature.

    When you invoke a function in bash, it’s executed within the context of the current shell, so it can access and modify all the variables.

    But a pipe is a list of processes which are connected with I/O pipes. That means your function is executed in a shell and only the output of this shell is visible to echo.

    Even exporting in myfunc wouldn’t work because export works only for processes started by the shell where you did the export and echo was started by the same shell as myfunc:

    bash
     +-- myfunc
     +-- echo
    

    that is echo is not a child of myfunc.

    Workarounds:

    1. Write the variable into a file
    2. Use a more complex output format like XML or several lines where the first line of output is always the variable and the real output comes in the next line.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This attempt to define a function overloaded for three sizes of integers fails. Why?
I'm currently having the following method which requires params - I cannot change this
Why does this attempt at creating a list of curried functions not work? def
This is my second attempt at this question . I made a mess of
I used this snippet to attempt full screen in Windows, and this is what
I'm using this code to attempt to convert a float to an int, then
Alright so I've been making an attempt at this for a while now. My
This is my first attempt at responsive design, so I'm keeping it simple. I
This is my first attempt to write shorthand if statements however am befuddled by
First attempt to use this cool site - after searching for 2 hours: So

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.