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

  • Home
  • SEARCH
  • 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 8464567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:47:09+00:00 2026-06-10T14:47:09+00:00

I tried exporting the function and then executing it with bash, but that doesn’t

  • 0

I tried exporting the function and then executing it with bash, but that doesn’t work:

$ export -f my_func
$ sudo bash -c 'my_func' 
bash: my_func: command not found

If I try to run the function with bash without sudo (bash -c ‘my_func’), it works.

Any idea?

  • 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-10T14:47:10+00:00Added an answer on June 10, 2026 at 2:47 pm

    Starting from the answer of bmargulies, I wrote a function to cover this issue, which basically realizes his idea.

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
    # EXESUDO
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
    #
    # Purpose:
    # -------------------------------------------------------------------- #
    # Execute a function with sudo
    #
    # Params:
    # -------------------------------------------------------------------- #
    # $1:   string: name of the function to be executed with sudo
    #
    # Usage:
    # -------------------------------------------------------------------- #
    # exesudo "funcname" followed by any param
    #
    # -------------------------------------------------------------------- #
    # Created 01 September 2012              Last Modified 02 September 2012
    
    function exesudo ()
    {
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
        #
        # LOCAL VARIABLES:
        #
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
        
        #
        # I use underscores to remember it's been passed
        local _funcname_="$1"
        
        local params=( "$@" )               ## array containing all params passed here
        local tmpfile="/dev/shm/$RANDOM"    ## temporary file
        local content                       ## content of the temporary file
        local regex                         ## regular expression
        
        
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
        #
        # MAIN CODE:
        #
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
        
        #
        # WORKING ON PARAMS:
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        #
        # Shift the first param (which is the name of the function)
        unset params[0]              ## remove first element
        # params=( "${params[@]}" )     ## repack array
        
        
        #
        # WORKING ON THE TEMPORARY FILE:
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        content="#!/bin/bash\n\n"
        
        #
        # Write the params array
        content="${content}params=(\n"
        
        regex="\s+"
        for param in "${params[@]}"
        do
            if [[ "$param" =~ $regex ]]
                then
                    content="${content}\t\"${param}\"\n"
                else
                    content="${content}\t${param}\n"
            fi
        done
        
        content="$content)\n"
        echo -e "$content" > "$tmpfile"
        
        #
        # Append the function source
        echo "#$( type "$_funcname_" )" >> "$tmpfile"
        
        #
        # Append the call to the function
        echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"
        
    
        #
        # DONE: EXECUTE THE TEMPORARY FILE WITH SUDO
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        sudo bash "$tmpfile"
        rm "$tmpfile"
    }
    

    Example of usage:

    running the following snippet

    #!/bin/bash
    
    function exesudo ()
    {
        # copy here the previous exesudo function !!!
    }
    
    test_it_out ()
    {
        local params=( "$@" )
        echo "Hello "$( whoami )"!"
        echo "You passed the following params:"
        printf "%s\n" "${params[@]}" ## print array
    }
    
    echo "1. calling without sudo"
    test_it_out "first" "second"
    
    echo ""
    echo "2. calling with sudo"
    exesudo test_it_out -n "john done" -s "done"
    
    exit
    

    Will output

    1. calling without sudo
      Hello yourname!
      You passed the following params:
      first
      second

    2. calling with sudo
      Hello root!
      You passed the following params:
      -n
      john done
      -s
      foo

    If you need to use this in a shell calling a function which is defined in your bashrc, as asked with a similar question on serverfault by another user, then you have to put the previous exesudo function on the same bashrc file as well, like the following:

    function yourfunc ()
    {
    echo "Hello "$( whoami )"!"
    }
    export -f yourfunc
    
    function exesudo ()
    {
       # copy here
    }
    export -f exesudo
    

    Then you have to logout and login again or use

    source ~/.bashrc
    

    Finally you can use exesudo as follow:

    $ yourfunc
    Hello yourname!
    
    $ exesudo yourfunc
    Hello root!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sort function which was working fine but then I tried to
Tried a bunch of things but I can't get it to work consistently amid
I have a Unix bash function that executes a script that parses custom environment
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
tried to find the answer by googling and in MSDN but with no luck.
I don't know why but I thought I could statically link a function from
I have a Delphi generic class that exposes a function with an argument of
I'm trying to create a user defined function in Oracle that will return a

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.