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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:54:41+00:00 2026-06-12T20:54:41+00:00

On Bash-Hackers.org there is a nice little wiki entry on collapsing functions. Basically, a

  • 0

On Bash-Hackers.org there is a nice little wiki entry on collapsing functions. Basically, a collapsing function is a function that redefines itself based on some condition. The basic example was something like the following:

chatter() {
  if [[ $verbose ]]; then
    chatter() {
      echo "$@"
    }
    chatter "$@"
  else
    chatter() {
      :
    }
  fi
}

I thought that was a nice little trick that might be useful for creating functions like the following:

# a portable extended regular expression sed for Linux and Mac
# simply checks if using an option fails for one version
# @see http://wiki.bash-hackers.org/howto/collapsing_functions
my_sed() {
    if ( echo abc | sed -r /abd/p > /dev/null 2>/dev/null ) ; then 
        #we are running a GNU version. redefine function
        echo gnu
        my_sed() {
            sed -r "$@"
        }
    else 
        #we are running another version. defaulting to BSD
        #redefining function
        echo osx
        my_sed() {
            sed -E "$@"
        }
    fi
    my_sed "$@"
}

(The echo statements are just for debugging btw). This works as intended when running it as my_sed "s/foo/bar" /tmp/somefile.txt, outputting “gnu” (on Linux, osx on Mac) the first time, then keeping silent for subsequent runs. But if I simply use the function in pipes, the function is not being redefined, constantly outputting “gnu”. Example: echo 123 | my_sed 's/foo/bar' will output “gnu” every time it is being run in the shell.

Why is this? What is piping doing with the current context/shell that keeps the function from keeping its new definition? Is piping forking a new process each time, so that the new definition is lost from the originating shell?

  • 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-12T20:54:42+00:00Added an answer on June 12, 2026 at 8:54 pm

    pipelines are run in a subshell, and the redefinition is lost when the subshell exits.

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

Sidebar

Related Questions

Following the bash-hackers wiki's recommendation , I want to edit files using ed. In
Question: I have a question that is apparently not answered by this already-asked Bash
Using bash I have a string that holds my deployed applications: APPS=myapp-a-1.0.war myapp-b-1.1.war myapp-c-1.2-SNAPSHOT.war
In bash when I run a command like wc & or cat & that
In bash I have a file called temporary.txt that contains one line of text.
My bash scripting is weak. I want to create a script that filters and
Simple Bash +1 please Hey there. I know I'm missing something simple in this
In bash on Mac OSX, I have a file (test case) that contains the
In bash I would like to extract part of many filenames and save that
#!/bin/bash for((i=1;i <=10;i++)) do php /var/www/get.php done I have the above shell script that

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.