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 programHow can I extract a function from bash/zsh with all its shell-related dependencies?Why is Bash automatically inserting `--color=auto` in `ls` command used in function

Should a soda bottle be stored horizontally or vertically?

A Riddle of Opportunity

What made the Tusken Raiders unable / unwilling to shoot down Luke's Landspeeder?

Is runnable pseudo code shunned?

Keep password in macro

How much do I need to invest monthly to accumulate a given amount?

Is Absorb Elements cast before or after a save? Is it cast before or after damage?

How could pirates reasonably transport dinosaurs in captivity, some of them enormous, across oceans?

Do any countries have a procedure that allows a constituent part of that country to become independent unilaterally?

Selective reduction of nitro group to amine, in benzene ring containing nitrile?

SQL Server: Multiple Availability Groups, one cluster, how do I keep the primary cluster node as primary?

Where to start with a child learning chess?

When is TeX better than LaTeX?

Fast cooking bone broth

Computer power supplies usually have higher efficiency on 230V than on 115V. Why?

Why do cargo airlines frequently choose passenger aircraft rather than aircraft designed specifically for cargo?

How many times, are they multiples?

Can we reduce power consumption of digital interfaces by using high impedance transmission lines?

How to compute the cohomology of a local system?

How do I use an .img.xz file or get an .img file from it?

Simple Markdown viewer for Windows

Can be a natural language be non-serializable?

Best spot within a human to place redundant heart

Why is the 'anti' in 'anti-semitism'?



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 programHow can I extract a function from bash/zsh with all its shell-related dependencies?Why 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;









12


















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 Bye; 


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










share|improve this question






















  • 2





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

    – mosvy
    Sep 26 at 7:25






  • 3





    or readonly -f f

    – mosvy
    Sep 26 at 7:26

















12


















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 Bye; 


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










share|improve this question






















  • 2





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

    – mosvy
    Sep 26 at 7:25






  • 3





    or readonly -f f

    – mosvy
    Sep 26 at 7:26













12













12









12


3






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 Bye; 


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 Bye; 


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 Sep 27 at 9:52







kyb

















asked Sep 26 at 7:23









kybkyb

3062 silver badges15 bronze badges




3062 silver badges15 bronze badges










  • 2





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

    – mosvy
    Sep 26 at 7:25






  • 3





    or readonly -f f

    – mosvy
    Sep 26 at 7:26












  • 2





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

    – mosvy
    Sep 26 at 7:25






  • 3





    or readonly -f f

    – mosvy
    Sep 26 at 7:26







2




2





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

– mosvy
Sep 26 at 7:25





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

– mosvy
Sep 26 at 7:25




3




3





or readonly -f f

– mosvy
Sep 26 at 7:26





or readonly -f f

– mosvy
Sep 26 at 7:26










1 Answer
1






active

oldest

votes


















25



















You may declare f as a read-only function using readonly -f f or declare -g -r -f f (readonly is equivalent to declare -g -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 Bye;
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).




Currently (as of bash-5.0.11), trying to modify a read-only function would not terminate the shell if one is using the errexit shell option (set -e). Chet, the bash maintainer, says that this is an oversight and that it will be changed with the next release.






share|improve this answer



























  • Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

    – kyb
    Sep 27 at 9:55











  • @kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

    – Kusalananda
    Sep 27 at 10:03











  • good, please update your answer when you will be sure about this behavior.

    – kyb
    Sep 27 at 14:19






  • 1





    @kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

    – Kusalananda
    Sep 27 at 20:54












  • @kyb I'm also noticing that you never say anything about errexit or set -e in your question.

    – Kusalananda
    Sep 27 at 20:58












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









25



















You may declare f as a read-only function using readonly -f f or declare -g -r -f f (readonly is equivalent to declare -g -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 Bye;
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).




Currently (as of bash-5.0.11), trying to modify a read-only function would not terminate the shell if one is using the errexit shell option (set -e). Chet, the bash maintainer, says that this is an oversight and that it will be changed with the next release.






share|improve this answer



























  • Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

    – kyb
    Sep 27 at 9:55











  • @kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

    – Kusalananda
    Sep 27 at 10:03











  • good, please update your answer when you will be sure about this behavior.

    – kyb
    Sep 27 at 14:19






  • 1





    @kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

    – Kusalananda
    Sep 27 at 20:54












  • @kyb I'm also noticing that you never say anything about errexit or set -e in your question.

    – Kusalananda
    Sep 27 at 20:58















25



















You may declare f as a read-only function using readonly -f f or declare -g -r -f f (readonly is equivalent to declare -g -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 Bye;
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).




Currently (as of bash-5.0.11), trying to modify a read-only function would not terminate the shell if one is using the errexit shell option (set -e). Chet, the bash maintainer, says that this is an oversight and that it will be changed with the next release.






share|improve this answer



























  • Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

    – kyb
    Sep 27 at 9:55











  • @kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

    – Kusalananda
    Sep 27 at 10:03











  • good, please update your answer when you will be sure about this behavior.

    – kyb
    Sep 27 at 14:19






  • 1





    @kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

    – Kusalananda
    Sep 27 at 20:54












  • @kyb I'm also noticing that you never say anything about errexit or set -e in your question.

    – Kusalananda
    Sep 27 at 20:58













25















25











25









You may declare f as a read-only function using readonly -f f or declare -g -r -f f (readonly is equivalent to declare -g -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 Bye;
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).




Currently (as of bash-5.0.11), trying to modify a read-only function would not terminate the shell if one is using the errexit shell option (set -e). Chet, the bash maintainer, says that this is an oversight and that it will be changed with the next release.






share|improve this answer
















You may declare f as a read-only function using readonly -f f or declare -g -r -f f (readonly is equivalent to declare -g -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 Bye;
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).




Currently (as of bash-5.0.11), trying to modify a read-only function would not terminate the shell if one is using the errexit shell option (set -e). Chet, the bash maintainer, says that this is an oversight and that it will be changed with the next release.







share|improve this answer















share|improve this answer




share|improve this answer








edited Oct 1 at 20:20

























answered Sep 26 at 7:25









KusalanandaKusalananda

175k20 gold badges333 silver badges539 bronze badges




175k20 gold badges333 silver badges539 bronze badges















  • Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

    – kyb
    Sep 27 at 9:55











  • @kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

    – Kusalananda
    Sep 27 at 10:03











  • good, please update your answer when you will be sure about this behavior.

    – kyb
    Sep 27 at 14:19






  • 1





    @kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

    – Kusalananda
    Sep 27 at 20:54












  • @kyb I'm also noticing that you never say anything about errexit or set -e in your question.

    – Kusalananda
    Sep 27 at 20:58

















  • Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

    – kyb
    Sep 27 at 9:55











  • @kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

    – Kusalananda
    Sep 27 at 10:03











  • good, please update your answer when you will be sure about this behavior.

    – kyb
    Sep 27 at 14:19






  • 1





    @kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

    – Kusalananda
    Sep 27 at 20:54












  • @kyb I'm also noticing that you never say anything about errexit or set -e in your question.

    – Kusalananda
    Sep 27 at 20:58
















Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

– kyb
Sep 27 at 9:55





Attempt to override function produces message bash: f: readonly function and non-zero status code, but does not exit if errexit option enabled.

– kyb
Sep 27 at 9:55













@kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

– Kusalananda
Sep 27 at 10:03





@kyb I noticed this as well. I'm not certain it's a bug in bash, but I will ask on one of the bash mailing lists to be sure.

– Kusalananda
Sep 27 at 10:03













good, please update your answer when you will be sure about this behavior.

– kyb
Sep 27 at 14:19





good, please update your answer when you will be sure about this behavior.

– kyb
Sep 27 at 14:19




1




1





@kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

– Kusalananda
Sep 27 at 20:54






@kyb Both Stephane Chazelas and Greg Wooledge weighed in on that question, both with plausible explanations. Stephane suggests that bash only exits when set -e is in effect when POSIX requires it to (and readonly -f is not POSIX). Greg points out that the bash manual never mentions "failure in function declaration" as reason for errexit to trigger an exit (unless a function declaration counts as a compound command, which he's pretty sure it does not). The thread is ongoing here: lists.gnu.org/archive/html/help-bash/2019-09/msg00039.html

– Kusalananda
Sep 27 at 20:54














@kyb I'm also noticing that you never say anything about errexit or set -e in your question.

– Kusalananda
Sep 27 at 20:58





@kyb I'm also noticing that you never say anything about errexit or set -e in your question.

– Kusalananda
Sep 27 at 20:58


















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

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?