Print name if parameter passed to functionFunction to evaluate variables in BASHHow do I get the value of a named option of an already running process in Linux?Conditional execution block with || and parentheses problemShell valid function name charactersWhile loop with result from function - BASHGet specific result from functionBash function assign value to passed parameterFunction to iterate over arrayHow am I allowed to pass a void parameter through bash functions?Pass parameter to Bash function which will serve as a pattern to awk
Why do bosons tend to occupy the same state?
Why can't we play rap on piano?
How seriously should I take size and weight limits of hand luggage?
Is it possible to download Internet Explorer on my Mac running OS X El Capitan?
Why doesn't using multiple commands with a || or && conditional work?
Dealing with conflict between co-workers for non-work-related issue affecting their work
Is there a way to turn 1.0 into a integer (1) while the same function ignores 1.5 and leaves it as a float
Is it possible to create a QR code using text?
One verb to replace 'be a member of' a club
How do I deal with an unproductive colleague in a small company?
Why do I get two different answers for this counting problem?
What is the most common color to indicate the input-field is disabled?
Does casting Light, or a similar spell, have any effect when the caster is swallowed by a monster?
What killed these X2 caps?
ssTTsSTtRrriinInnnnNNNIiinngg
Should I tell management that I intend to leave due to bad software development practices?
Aircraft with solar-panels?
Little known, relatively unlikely, but scientifically plausible, apocalyptic (or near apocalyptic) events
Infinite Abelian subgroup of infinite non Abelian group example
Twin primes whose sum is a cube
I would say: "You are another teacher", but she is a woman and I am a man
How could indestructible materials be used in power generation?
Why is consensus so controversial in Britain?
90's TV series where a boy goes to another dimension through portal near power lines
Print name if parameter passed to function
Function to evaluate variables in BASHHow do I get the value of a named option of an already running process in Linux?Conditional execution block with || and parentheses problemShell valid function name charactersWhile loop with result from function - BASHGet specific result from functionBash function assign value to passed parameterFunction to iterate over arrayHow am I allowed to pass a void parameter through bash functions?Pass parameter to Bash function which will serve as a pattern to awk
I have written a little function that exits if the value of the function argument is empty, I would like to be able to also print the name of the parameter (not the value!) if it is possible, my following implementation fails to print the name of the parameter.
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $!1 is empty"
exit 1
fi
when called like so
exitIfEmpty someKey
should print
Exiting because someKey is empty
bash
add a comment |
I have written a little function that exits if the value of the function argument is empty, I would like to be able to also print the name of the parameter (not the value!) if it is possible, my following implementation fails to print the name of the parameter.
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $!1 is empty"
exit 1
fi
when called like so
exitIfEmpty someKey
should print
Exiting because someKey is empty
bash
Wait, when you give it "someKey", $1 won't be empty. If it's empty, there's nothing to print.
– choroba
Mar 26 at 15:08
@choroba I want to print the parameter name if possible not its value.
– Xerxes
Mar 26 at 15:09
1
What do you mean by the name? Function arguments don't have names.
– choroba
Mar 26 at 15:32
add a comment |
I have written a little function that exits if the value of the function argument is empty, I would like to be able to also print the name of the parameter (not the value!) if it is possible, my following implementation fails to print the name of the parameter.
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $!1 is empty"
exit 1
fi
when called like so
exitIfEmpty someKey
should print
Exiting because someKey is empty
bash
I have written a little function that exits if the value of the function argument is empty, I would like to be able to also print the name of the parameter (not the value!) if it is possible, my following implementation fails to print the name of the parameter.
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $!1 is empty"
exit 1
fi
when called like so
exitIfEmpty someKey
should print
Exiting because someKey is empty
bash
bash
edited Mar 26 at 17:42
GAD3R
27.9k1958114
27.9k1958114
asked Mar 26 at 15:06
XerxesXerxes
1956
1956
Wait, when you give it "someKey", $1 won't be empty. If it's empty, there's nothing to print.
– choroba
Mar 26 at 15:08
@choroba I want to print the parameter name if possible not its value.
– Xerxes
Mar 26 at 15:09
1
What do you mean by the name? Function arguments don't have names.
– choroba
Mar 26 at 15:32
add a comment |
Wait, when you give it "someKey", $1 won't be empty. If it's empty, there's nothing to print.
– choroba
Mar 26 at 15:08
@choroba I want to print the parameter name if possible not its value.
– Xerxes
Mar 26 at 15:09
1
What do you mean by the name? Function arguments don't have names.
– choroba
Mar 26 at 15:32
Wait, when you give it "someKey", $1 won't be empty. If it's empty, there's nothing to print.
– choroba
Mar 26 at 15:08
Wait, when you give it "someKey", $1 won't be empty. If it's empty, there's nothing to print.
– choroba
Mar 26 at 15:08
@choroba I want to print the parameter name if possible not its value.
– Xerxes
Mar 26 at 15:09
@choroba I want to print the parameter name if possible not its value.
– Xerxes
Mar 26 at 15:09
1
1
What do you mean by the name? Function arguments don't have names.
– choroba
Mar 26 at 15:32
What do you mean by the name? Function arguments don't have names.
– choroba
Mar 26 at 15:32
add a comment |
3 Answers
3
active
oldest
votes
What gets passed to the function is just a string. If you run func somevar
, what is passed is the string somevar
. If you run func $somevar
, what is passed is (the word-split) value of the variable somevar
. Neither is a variable reference, a pointer or anything like that, they're just strings.
If you want to pass the name of a variable to a function, and then look at the value of that variable, you'll need to use a nameref (Bash 4.3 or later, IIRC), or an indirect reference $!var
. $!var
expands to the value of the variable whose name is stored in var
.
So, you just have it the wrong way in the script, if you pass the name of a variable to function, use "$!1"
to get the value of the variable named in $1
, and plain "$1"
to get the name.
E.g. this will print variable bar is empty, exiting
, and exit the shell:
#!/bin/bash
exitIfEmpty()
if [ -z "$!1" ]; then
echo "variable $1 is empty, exiting"
exit 1
fi
foo=x
unset bar
exitIfEmpty foo
exitIfEmpty bar
Also:: "$!1:?Variable $1 is empty or unset"
.
– Kusalananda♦
Mar 27 at 10:16
add a comment |
Pass the name as second argument
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $2 is empty"
exit 1
fi
exitIfEmpty "$someKey" someKey
add a comment |
echo "Exiting because $1 is empty"
should do the trick.
3
Wouldn't this always printExiting because is empty
, if the test is against$1
also? That doesn't seem very useful.
– ilkkachu
Mar 26 at 15:30
ITYMecho 'Exit because $1 is empty'
orecho "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require$
to be escaped
– jimbobmcgee
Mar 26 at 19:22
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/3.0/"u003ecc by-sa 3.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%2f508764%2fprint-name-if-parameter-passed-to-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
What gets passed to the function is just a string. If you run func somevar
, what is passed is the string somevar
. If you run func $somevar
, what is passed is (the word-split) value of the variable somevar
. Neither is a variable reference, a pointer or anything like that, they're just strings.
If you want to pass the name of a variable to a function, and then look at the value of that variable, you'll need to use a nameref (Bash 4.3 or later, IIRC), or an indirect reference $!var
. $!var
expands to the value of the variable whose name is stored in var
.
So, you just have it the wrong way in the script, if you pass the name of a variable to function, use "$!1"
to get the value of the variable named in $1
, and plain "$1"
to get the name.
E.g. this will print variable bar is empty, exiting
, and exit the shell:
#!/bin/bash
exitIfEmpty()
if [ -z "$!1" ]; then
echo "variable $1 is empty, exiting"
exit 1
fi
foo=x
unset bar
exitIfEmpty foo
exitIfEmpty bar
Also:: "$!1:?Variable $1 is empty or unset"
.
– Kusalananda♦
Mar 27 at 10:16
add a comment |
What gets passed to the function is just a string. If you run func somevar
, what is passed is the string somevar
. If you run func $somevar
, what is passed is (the word-split) value of the variable somevar
. Neither is a variable reference, a pointer or anything like that, they're just strings.
If you want to pass the name of a variable to a function, and then look at the value of that variable, you'll need to use a nameref (Bash 4.3 or later, IIRC), or an indirect reference $!var
. $!var
expands to the value of the variable whose name is stored in var
.
So, you just have it the wrong way in the script, if you pass the name of a variable to function, use "$!1"
to get the value of the variable named in $1
, and plain "$1"
to get the name.
E.g. this will print variable bar is empty, exiting
, and exit the shell:
#!/bin/bash
exitIfEmpty()
if [ -z "$!1" ]; then
echo "variable $1 is empty, exiting"
exit 1
fi
foo=x
unset bar
exitIfEmpty foo
exitIfEmpty bar
Also:: "$!1:?Variable $1 is empty or unset"
.
– Kusalananda♦
Mar 27 at 10:16
add a comment |
What gets passed to the function is just a string. If you run func somevar
, what is passed is the string somevar
. If you run func $somevar
, what is passed is (the word-split) value of the variable somevar
. Neither is a variable reference, a pointer or anything like that, they're just strings.
If you want to pass the name of a variable to a function, and then look at the value of that variable, you'll need to use a nameref (Bash 4.3 or later, IIRC), or an indirect reference $!var
. $!var
expands to the value of the variable whose name is stored in var
.
So, you just have it the wrong way in the script, if you pass the name of a variable to function, use "$!1"
to get the value of the variable named in $1
, and plain "$1"
to get the name.
E.g. this will print variable bar is empty, exiting
, and exit the shell:
#!/bin/bash
exitIfEmpty()
if [ -z "$!1" ]; then
echo "variable $1 is empty, exiting"
exit 1
fi
foo=x
unset bar
exitIfEmpty foo
exitIfEmpty bar
What gets passed to the function is just a string. If you run func somevar
, what is passed is the string somevar
. If you run func $somevar
, what is passed is (the word-split) value of the variable somevar
. Neither is a variable reference, a pointer or anything like that, they're just strings.
If you want to pass the name of a variable to a function, and then look at the value of that variable, you'll need to use a nameref (Bash 4.3 or later, IIRC), or an indirect reference $!var
. $!var
expands to the value of the variable whose name is stored in var
.
So, you just have it the wrong way in the script, if you pass the name of a variable to function, use "$!1"
to get the value of the variable named in $1
, and plain "$1"
to get the name.
E.g. this will print variable bar is empty, exiting
, and exit the shell:
#!/bin/bash
exitIfEmpty()
if [ -z "$!1" ]; then
echo "variable $1 is empty, exiting"
exit 1
fi
foo=x
unset bar
exitIfEmpty foo
exitIfEmpty bar
edited Mar 27 at 10:07
answered Mar 26 at 15:24
ilkkachuilkkachu
63k10103180
63k10103180
Also:: "$!1:?Variable $1 is empty or unset"
.
– Kusalananda♦
Mar 27 at 10:16
add a comment |
Also:: "$!1:?Variable $1 is empty or unset"
.
– Kusalananda♦
Mar 27 at 10:16
Also:
: "$!1:?Variable $1 is empty or unset"
.– Kusalananda♦
Mar 27 at 10:16
Also:
: "$!1:?Variable $1 is empty or unset"
.– Kusalananda♦
Mar 27 at 10:16
add a comment |
Pass the name as second argument
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $2 is empty"
exit 1
fi
exitIfEmpty "$someKey" someKey
add a comment |
Pass the name as second argument
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $2 is empty"
exit 1
fi
exitIfEmpty "$someKey" someKey
add a comment |
Pass the name as second argument
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $2 is empty"
exit 1
fi
exitIfEmpty "$someKey" someKey
Pass the name as second argument
function exitIfEmpty()
if [ -z "$1" ]
then
echo "Exiting because $2 is empty"
exit 1
fi
exitIfEmpty "$someKey" someKey
edited Mar 27 at 17:30
answered Mar 26 at 15:18
JShorthouseJShorthouse
52728
52728
add a comment |
add a comment |
echo "Exiting because $1 is empty"
should do the trick.
3
Wouldn't this always printExiting because is empty
, if the test is against$1
also? That doesn't seem very useful.
– ilkkachu
Mar 26 at 15:30
ITYMecho 'Exit because $1 is empty'
orecho "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require$
to be escaped
– jimbobmcgee
Mar 26 at 19:22
add a comment |
echo "Exiting because $1 is empty"
should do the trick.
3
Wouldn't this always printExiting because is empty
, if the test is against$1
also? That doesn't seem very useful.
– ilkkachu
Mar 26 at 15:30
ITYMecho 'Exit because $1 is empty'
orecho "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require$
to be escaped
– jimbobmcgee
Mar 26 at 19:22
add a comment |
echo "Exiting because $1 is empty"
should do the trick.
echo "Exiting because $1 is empty"
should do the trick.
answered Mar 26 at 15:07
PankiPanki
860512
860512
3
Wouldn't this always printExiting because is empty
, if the test is against$1
also? That doesn't seem very useful.
– ilkkachu
Mar 26 at 15:30
ITYMecho 'Exit because $1 is empty'
orecho "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require$
to be escaped
– jimbobmcgee
Mar 26 at 19:22
add a comment |
3
Wouldn't this always printExiting because is empty
, if the test is against$1
also? That doesn't seem very useful.
– ilkkachu
Mar 26 at 15:30
ITYMecho 'Exit because $1 is empty'
orecho "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require$
to be escaped
– jimbobmcgee
Mar 26 at 19:22
3
3
Wouldn't this always print
Exiting because is empty
, if the test is against $1
also? That doesn't seem very useful.– ilkkachu
Mar 26 at 15:30
Wouldn't this always print
Exiting because is empty
, if the test is against $1
also? That doesn't seem very useful.– ilkkachu
Mar 26 at 15:30
ITYM
echo 'Exit because $1 is empty'
or echo "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require $
to be escaped– jimbobmcgee
Mar 26 at 19:22
ITYM
echo 'Exit because $1 is empty'
or echo "Exit because $1 is empty"
-- single-quote strings don't do variable expansion; double-quoted strings require $
to be escaped– jimbobmcgee
Mar 26 at 19:22
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%2f508764%2fprint-name-if-parameter-passed-to-function%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
Wait, when you give it "someKey", $1 won't be empty. If it's empty, there's nothing to print.
– choroba
Mar 26 at 15:08
@choroba I want to print the parameter name if possible not its value.
– Xerxes
Mar 26 at 15:09
1
What do you mean by the name? Function arguments don't have names.
– choroba
Mar 26 at 15:32