Dynamic substitution of variables in bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Honestly I don't even know how to ask this, or what are the proper terms lol
So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
So what I had before was this
if [ -z ${file_var} ]; then
echo "empty argument"
fi
if [ -z ${pw_var} ]; then
echo "empty argument"
fi
So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:
for i in file_var, pw_var;do
if [ -z ${${i}} ]; then
echo "empty argument"
fi
done
Now this obviously doesn't work, so I'm wondering how is it properly done.
bash
add a comment |
Honestly I don't even know how to ask this, or what are the proper terms lol
So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
So what I had before was this
if [ -z ${file_var} ]; then
echo "empty argument"
fi
if [ -z ${pw_var} ]; then
echo "empty argument"
fi
So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:
for i in file_var, pw_var;do
if [ -z ${${i}} ]; then
echo "empty argument"
fi
done
Now this obviously doesn't work, so I'm wondering how is it properly done.
bash
add a comment |
Honestly I don't even know how to ask this, or what are the proper terms lol
So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
So what I had before was this
if [ -z ${file_var} ]; then
echo "empty argument"
fi
if [ -z ${pw_var} ]; then
echo "empty argument"
fi
So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:
for i in file_var, pw_var;do
if [ -z ${${i}} ]; then
echo "empty argument"
fi
done
Now this obviously doesn't work, so I'm wondering how is it properly done.
bash
Honestly I don't even know how to ask this, or what are the proper terms lol
So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
So what I had before was this
if [ -z ${file_var} ]; then
echo "empty argument"
fi
if [ -z ${pw_var} ]; then
echo "empty argument"
fi
So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:
for i in file_var, pw_var;do
if [ -z ${${i}} ]; then
echo "empty argument"
fi
done
Now this obviously doesn't work, so I'm wondering how is it properly done.
bash
bash
asked 5 hours ago
user323587user323587
542
542
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
What you're trying to do is indirect expansion, which Bash supports using !
:
if [ -z "${!i}" ] ; then
If the first character of parameter is an exclamation point (
!
), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
In this case, "${!i}"
expands to whatever the value of the variable whose name is stored in i
, and -z
will test that indirectly-accessed value.
Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name
, and thereafter
All references, assignments, and attribute modifications to
name
, except for those using or changing the-n
attribute itself, are performed on the variable referenced byname
’s value.
So if you add
declare -n i
before your loop, it will mean that just
for i in file_var pw_var
do
if [ -z "$i" ]
then
...
fi
done
will work as you wanted. Do note though that assignments to i
(as opposed to its being set in a loop like this) will modify the target variable.
Note also that there is no comma between items you're looping over in a Bash for
loop like how you've written it in the question.
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
add a comment |
This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.
file_var=""
pw_var=""
for i in 'file_var' 'pw_var';do
if [ -z ${!i} ]; then
echo "$i is empty"
fi
done
New contributor
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2f512008%2fdynamic-substitution-of-variables-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you're trying to do is indirect expansion, which Bash supports using !
:
if [ -z "${!i}" ] ; then
If the first character of parameter is an exclamation point (
!
), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
In this case, "${!i}"
expands to whatever the value of the variable whose name is stored in i
, and -z
will test that indirectly-accessed value.
Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name
, and thereafter
All references, assignments, and attribute modifications to
name
, except for those using or changing the-n
attribute itself, are performed on the variable referenced byname
’s value.
So if you add
declare -n i
before your loop, it will mean that just
for i in file_var pw_var
do
if [ -z "$i" ]
then
...
fi
done
will work as you wanted. Do note though that assignments to i
(as opposed to its being set in a loop like this) will modify the target variable.
Note also that there is no comma between items you're looping over in a Bash for
loop like how you've written it in the question.
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
add a comment |
What you're trying to do is indirect expansion, which Bash supports using !
:
if [ -z "${!i}" ] ; then
If the first character of parameter is an exclamation point (
!
), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
In this case, "${!i}"
expands to whatever the value of the variable whose name is stored in i
, and -z
will test that indirectly-accessed value.
Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name
, and thereafter
All references, assignments, and attribute modifications to
name
, except for those using or changing the-n
attribute itself, are performed on the variable referenced byname
’s value.
So if you add
declare -n i
before your loop, it will mean that just
for i in file_var pw_var
do
if [ -z "$i" ]
then
...
fi
done
will work as you wanted. Do note though that assignments to i
(as opposed to its being set in a loop like this) will modify the target variable.
Note also that there is no comma between items you're looping over in a Bash for
loop like how you've written it in the question.
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
add a comment |
What you're trying to do is indirect expansion, which Bash supports using !
:
if [ -z "${!i}" ] ; then
If the first character of parameter is an exclamation point (
!
), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
In this case, "${!i}"
expands to whatever the value of the variable whose name is stored in i
, and -z
will test that indirectly-accessed value.
Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name
, and thereafter
All references, assignments, and attribute modifications to
name
, except for those using or changing the-n
attribute itself, are performed on the variable referenced byname
’s value.
So if you add
declare -n i
before your loop, it will mean that just
for i in file_var pw_var
do
if [ -z "$i" ]
then
...
fi
done
will work as you wanted. Do note though that assignments to i
(as opposed to its being set in a loop like this) will modify the target variable.
Note also that there is no comma between items you're looping over in a Bash for
loop like how you've written it in the question.
What you're trying to do is indirect expansion, which Bash supports using !
:
if [ -z "${!i}" ] ; then
If the first character of parameter is an exclamation point (
!
), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
In this case, "${!i}"
expands to whatever the value of the variable whose name is stored in i
, and -z
will test that indirectly-accessed value.
Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name
, and thereafter
All references, assignments, and attribute modifications to
name
, except for those using or changing the-n
attribute itself, are performed on the variable referenced byname
’s value.
So if you add
declare -n i
before your loop, it will mean that just
for i in file_var pw_var
do
if [ -z "$i" ]
then
...
fi
done
will work as you wanted. Do note though that assignments to i
(as opposed to its being set in a loop like this) will modify the target variable.
Note also that there is no comma between items you're looping over in a Bash for
loop like how you've written it in the question.
edited 5 hours ago
answered 5 hours ago
Michael HomerMichael Homer
50.8k8141177
50.8k8141177
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
add a comment |
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Thank you! That's exactly what I needed! Also thanks for the for loop tip :D
– user323587
5 hours ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
Namerefs are not supported on some older bashes, so use them carefully.
– D. Ben Knoble
33 mins ago
add a comment |
This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.
file_var=""
pw_var=""
for i in 'file_var' 'pw_var';do
if [ -z ${!i} ]; then
echo "$i is empty"
fi
done
New contributor
add a comment |
This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.
file_var=""
pw_var=""
for i in 'file_var' 'pw_var';do
if [ -z ${!i} ]; then
echo "$i is empty"
fi
done
New contributor
add a comment |
This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.
file_var=""
pw_var=""
for i in 'file_var' 'pw_var';do
if [ -z ${!i} ]; then
echo "$i is empty"
fi
done
New contributor
This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.
file_var=""
pw_var=""
for i in 'file_var' 'pw_var';do
if [ -z ${!i} ]; then
echo "$i is empty"
fi
done
New contributor
New contributor
answered 5 hours ago
Bob DoleBob Dole
661
661
New contributor
New contributor
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2f512008%2fdynamic-substitution-of-variables-in-bash%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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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