How to protect bash function from being overridden?Bash Function DecoratorInclude a bash function into the parent scriptBash Scripting echo locally in a functionDefining bash function dynamically using evalBash function call from script file fails, while call from terminal succeedsHow to export all Bash functions in a file in one line?How to make my bash function known to external programWhy is Bash automatically inserting `--color=auto` in `ls` command used in function

Notation clarity question for a conglomerate of accidentals

"Tenersi pronto" = to get ready or to be ready

Manager told a colleague of mine I was getting fired soon

Why do popular TCP-using services have UDP as well as TCP entries in /etc/services?

Where does the image of a data connector as a sharp metal spike originate from?

How dangerous are my worn rims?

How to say "respectively" in German when listing (enumerating) things

Single tx included in two different blocks

Booting Ubuntu from USB drive on MSI motherboard -- EVERYTHING fails

As a team leader is it appropriate to bring in fundraiser candy?

Quote to show students don't have to fear making mistakes

Is it appropriate to "shop" through high-impact journals before sending the paper to more specialized journals?

Avoiding dust scattering when you drill

French license plates

How to identify whether a publisher is genuine or not?

Can Fabled Passage generate two mana with Amulet of Vigor?

If I travelled back in time to invest in X company to make a fortune, roughly what is the probability that it would fail?

Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?

Why is there such a singular place for bird watching?

How are proofs normally constructed in a write up, in one line or split up into multiple lines?

Looking for circuit board material that can be dissolved

The answer is a girl's name (my future granddaughter) - can anyone help?

What is the difference between increasing volume and increasing gain?

What's the correct way to determine turn order in this situation?



How to protect bash function from being overridden?


Bash Function DecoratorInclude a bash function into the parent scriptBash Scripting echo locally in a functionDefining bash function dynamically using evalBash function call from script file fails, while call from terminal succeedsHow to export all Bash functions in a file in one line?How to make my bash function known to external programWhy is Bash automatically inserting `--color=auto` in `ls` command used in function






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









4















In the bash shell, we can define a function f with



f() echo Hello; 


and then redeclare/override it, without any error or warning messages, with



f() echo Buy; 


I believe there is a way to protect functions from being overridden in this way.










share|improve this question





















  • 1





    the same as with variables, with typeset -r: typeset -rf f.

    – mosvy
    10 hours ago






  • 2





    or readonly -f f

    – mosvy
    10 hours ago

















4















In the bash shell, we can define a function f with



f() echo Hello; 


and then redeclare/override it, without any error or warning messages, with



f() echo Buy; 


I believe there is a way to protect functions from being overridden in this way.










share|improve this question





















  • 1





    the same as with variables, with typeset -r: typeset -rf f.

    – mosvy
    10 hours ago






  • 2





    or readonly -f f

    – mosvy
    10 hours ago













4












4








4








In the bash shell, we can define a function f with



f() echo Hello; 


and then redeclare/override it, without any error or warning messages, with



f() echo Buy; 


I believe there is a way to protect functions from being overridden in this way.










share|improve this question
















In the bash shell, we can define a function f with



f() echo Hello; 


and then redeclare/override it, without any error or warning messages, with



f() echo Buy; 


I believe there is a way to protect functions from being overridden in this way.







bash bash-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 50 mins ago









Kusalananda

166k20 gold badges323 silver badges516 bronze badges




166k20 gold badges323 silver badges516 bronze badges










asked 10 hours ago









kybkyb

1661 silver badge11 bronze badges




1661 silver badge11 bronze badges










  • 1





    the same as with variables, with typeset -r: typeset -rf f.

    – mosvy
    10 hours ago






  • 2





    or readonly -f f

    – mosvy
    10 hours ago












  • 1





    the same as with variables, with typeset -r: typeset -rf f.

    – mosvy
    10 hours ago






  • 2





    or readonly -f f

    – mosvy
    10 hours ago







1




1





the same as with variables, with typeset -r: typeset -rf f.

– mosvy
10 hours ago





the same as with variables, with typeset -r: typeset -rf f.

– mosvy
10 hours ago




2




2





or readonly -f f

– mosvy
10 hours ago





or readonly -f f

– mosvy
10 hours ago










1 Answer
1






active

oldest

votes


















9
















You may declare f as a read-only function using readonly -f f or declare -r -f f (readonly is equivalent to declare -r). It's the -f option to these built-in utilities that makes them act on f as the name of a function, rather than on the variable f.



$ f() echo Hello; 
$ readonly -f f
$ f() echo Buy;
bash: f: readonly function
$ unset -f f
bash: unset: f: cannot unset: readonly function
$ f
Hello


As you can see, making the function read-only not only protects it from getting overridden, but also protects it from being unset (removed completely).






share|improve this answer




























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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/4.0/"u003ecc by-sa 4.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
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f543792%2fhow-to-protect-bash-function-from-being-overridden%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









    9
















    You may declare f as a read-only function using readonly -f f or declare -r -f f (readonly is equivalent to declare -r). It's the -f option to these built-in utilities that makes them act on f as the name of a function, rather than on the variable f.



    $ f() echo Hello; 
    $ readonly -f f
    $ f() echo Buy;
    bash: f: readonly function
    $ unset -f f
    bash: unset: f: cannot unset: readonly function
    $ f
    Hello


    As you can see, making the function read-only not only protects it from getting overridden, but also protects it from being unset (removed completely).






    share|improve this answer































      9
















      You may declare f as a read-only function using readonly -f f or declare -r -f f (readonly is equivalent to declare -r). It's the -f option to these built-in utilities that makes them act on f as the name of a function, rather than on the variable f.



      $ f() echo Hello; 
      $ readonly -f f
      $ f() echo Buy;
      bash: f: readonly function
      $ unset -f f
      bash: unset: f: cannot unset: readonly function
      $ f
      Hello


      As you can see, making the function read-only not only protects it from getting overridden, but also protects it from being unset (removed completely).






      share|improve this answer





























        9














        9










        9









        You may declare f as a read-only function using readonly -f f or declare -r -f f (readonly is equivalent to declare -r). It's the -f option to these built-in utilities that makes them act on f as the name of a function, rather than on the variable f.



        $ f() echo Hello; 
        $ readonly -f f
        $ f() echo Buy;
        bash: f: readonly function
        $ unset -f f
        bash: unset: f: cannot unset: readonly function
        $ f
        Hello


        As you can see, making the function read-only not only protects it from getting overridden, but also protects it from being unset (removed completely).






        share|improve this answer















        You may declare f as a read-only function using readonly -f f or declare -r -f f (readonly is equivalent to declare -r). It's the -f option to these built-in utilities that makes them act on f as the name of a function, rather than on the variable f.



        $ f() echo Hello; 
        $ readonly -f f
        $ f() echo Buy;
        bash: f: readonly function
        $ unset -f f
        bash: unset: f: cannot unset: readonly function
        $ f
        Hello


        As you can see, making the function read-only not only protects it from getting overridden, but also protects it from being unset (removed completely).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 46 mins ago

























        answered 10 hours ago









        KusalanandaKusalananda

        166k20 gold badges323 silver badges516 bronze badges




        166k20 gold badges323 silver badges516 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f543792%2fhow-to-protect-bash-function-from-being-overridden%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

            Sahara Skak | Bilen | Luke uk diar | NawigatsjuunCommonskategorii: SaharaWikivoyage raisfeerer: Sahara26° N, 13° O

            The fall designs the understood secretary. Looking glass Science Shock Discovery Hot Everybody Loves Raymond Smile 곳 서비스 성실하다 Defas Kaloolon Definition: To combine or impregnate with sulphur or any of its compounds as to sulphurize caoutchouc in vulcanizing Flame colored Reason Useful Thin Help 갖다 유명하다 낙엽 장례식 Country Iron Definition: A fencer a gladiator one who exhibits his skill in the use of the sword Definition: The American black throated bunting Spiza Americana Nostalgic Needy Method to my madness 시키다 평가되다 전부 소설가 우아하다 Argument Tin Feeling Representative Gym Music Gaur Chicken 일쑤 코치 편 학생증 The harbor values the sugar. Vasagle Yammoe Enstatite Definition: Capable of being limited Road Neighborly Five Refer Built Kangaroo 비비다 Degree Release Bargain Horse 하루 형님 유교 석 동부 괴롭히다 경제력

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу