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;
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
add a comment
|
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
1
the same as with variables, withtypeset -r:typeset -rf f.
– mosvy
10 hours ago
2
orreadonly -f f
– mosvy
10 hours ago
add a comment
|
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
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
bash bash-functions
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, withtypeset -r:typeset -rf f.
– mosvy
10 hours ago
2
orreadonly -f f
– mosvy
10 hours ago
add a comment
|
1
the same as with variables, withtypeset -r:typeset -rf f.
– mosvy
10 hours ago
2
orreadonly -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
add a comment
|
1 Answer
1
active
oldest
votes
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).
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
add a comment
|
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).
add a comment
|
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).
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).
edited 46 mins ago
answered 10 hours ago
Kusalananda♦Kusalananda
166k20 gold badges323 silver badges516 bronze badges
166k20 gold badges323 silver badges516 bronze badges
add a comment
|
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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