[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: looping over variables & exporting them at the same time ?
From: |
Zachary Santer |
Subject: |
Re: looping over variables & exporting them at the same time ? |
Date: |
Sun, 28 Jan 2024 22:11:43 -0500 |
On Sat, Jan 27, 2024 at 2:07 PM Paxsali via <help-bash@gnu.org> wrote:
> Have you tried "name reference" variables?
>
> help declare # see -n option
>
> Example:
>
> A='hello'
> B='world'
>
> # show the properties and content of the variables (BEFORE):
> echo "${A@A}"
> echo "${B@A}"
>
> # output (BEFORE):
> # A='hello'
> # B='world'
>
> declare -n var
> for var in A B; do
> var="$(printf '%s' "${var}" | tr 'o' '!')"
> export var
> done
>
> # show the properties and content of the variables (AFTER):
> echo "${A@A}"
> echo "${B@A}"
>
> # output:
> # declare -x A='hell!'
> # declare -x B='w!rld'
>
> the "declare -x" means the variable is exported.
> the content has changed according to the manipulation (in my example "tr"
> instead of "sed", but you can do whatever).
>
> is that what you were looking for?
>
Overall, I like Paxsali's solution, but piping the contents of a variable
into sed or tr is kind of ridiculous in this situation. There's a parameter
expansion for that.
Additionally, if you want to ensure that you only remove the full word
"xtest" from your string variables by searching for it surrounded by
spaces, it better be surrounded by spaces in the variable, even if it's the
first or last word in the variable.
#!/bin/bash
A='this is a test xtest of A str'
B='this is a test xtest of B str'
printf '%s\n' "Before:"
printf '%s\n' "${A@A}"
printf '%s\n' "${B@A}"
declare -n var
for var in A B; do
# Surround the contents of var with spaces
var=" ${var} "
# Replace all instances of " xtest " in the variable with " "
var="${var// xtest / }"
# Remove the leading space we added
var="${var# }"
# Remove the trailing space we added
var="${var% }"
export var
done
printf '%s\n' "After:"
printf '%s\n' "${A@A}"
printf '%s\n' "${B@A}"
Before:
A='this is a test xtest of A str'
B='this is a test xtest of B str'
After:
declare -x A='this is a test of A str'
declare -x B='this is a test of B str'
I'm also curious about the actual use-case here.
- looping over variables & exporting them at the same time ?, heavy . gear5130, 2024/01/27
- Re: looping over variables & exporting them at the same time ?, Greg Wooledge, 2024/01/27
- Re: looping over variables & exporting them at the same time ?, Paxsali, 2024/01/27
- Re: looping over variables & exporting them at the same time ?,
Zachary Santer <=
- Re: looping over variables & exporting them at the same time ?, Greg Wooledge, 2024/01/28
- Re: looping over variables & exporting them at the same time ?, Zachary Santer, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Greg Wooledge, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, alex xmb sw ratchev, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Paxsali, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Paxsali, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Greg Wooledge, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Paxsali, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Greg Wooledge, 2024/01/29
- Re: looping over variables & exporting them at the same time ?, Zachary Santer, 2024/01/29