Replace environment variables in text if they exist
Clash Royale CLAN TAG#URR8PPP
I know that envsubst
replaces declared environment variables in the input.
$ echo 'Hello $USER' | envsubst
Hello myusername
What I want is a way to replace the environment variable if it exists otherwise envsusbst
(or any other command), leaves the variable string as it is. What I get is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
Hello myusername
What I want is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
Hello myusername $UNDEFINED_VARIABLE
linux bash shell
add a comment |
I know that envsubst
replaces declared environment variables in the input.
$ echo 'Hello $USER' | envsubst
Hello myusername
What I want is a way to replace the environment variable if it exists otherwise envsusbst
(or any other command), leaves the variable string as it is. What I get is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
Hello myusername
What I want is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
Hello myusername $UNDEFINED_VARIABLE
linux bash shell
add a comment |
I know that envsubst
replaces declared environment variables in the input.
$ echo 'Hello $USER' | envsubst
Hello myusername
What I want is a way to replace the environment variable if it exists otherwise envsusbst
(or any other command), leaves the variable string as it is. What I get is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
Hello myusername
What I want is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
Hello myusername $UNDEFINED_VARIABLE
linux bash shell
I know that envsubst
replaces declared environment variables in the input.
$ echo 'Hello $USER' | envsubst
Hello myusername
What I want is a way to replace the environment variable if it exists otherwise envsusbst
(or any other command), leaves the variable string as it is. What I get is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst
Hello myusername
What I want is:
$ echo 'Hello $USER $UNDEFINED_VARIABLE' | somecommand
Hello myusername $UNDEFINED_VARIABLE
linux bash shell
linux bash shell
asked Jan 6 at 8:49
Farzad VertigoFarzad Vertigo
1133
1133
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It you pass an argument like $USER$PATH
to envsubst
, then it expands only those variables that are referenced in that argument.
So one way could be to pass it all the currently defined environment variables in that format. With zsh
:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst $$(kj:$:)parameters[(R)*export*]
$parameters
is a special associative array that maps variable names to their type$parameters[(R)*export*]
expands to all the elements of the associative array whose value containsexport
.- with the
k
parameter expansion flag, the key instead of the value is returned j:$:
joins those elements with$
in between, and we add one at the start.
With other shells, you can always revert to perl
to get that list:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"
Beware both disclose your environment variable names in the output of ps
.
Instead, you could also do the whole thing in perl
:
perl -pe 's(?$ENV$1//$&ge'
Beware it has the same limitations as envsubst
in that it won't expand things like $VAR:-x
and would expand $HOME
in things like $HOME
or $$HOME
which a shell wouldn't.
add a comment |
You can use env
to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)
echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"
(The output of env
lists the values of the variables as well, but envsubst
also wants to see a leading $
, so we can't just use cut -d= -f1
on its own, unfortunately. You could use a single sed
to do cut
's job as well, see previous revision, but I prefer the clarity of cut
over a tiny performance gain.)
That assumes none of the env variables names or values contain newline characters (and names don't contain$
characters)
– Stéphane Chazelas
Jan 6 at 9:17
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%2f492772%2freplace-environment-variables-in-text-if-they-exist%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
It you pass an argument like $USER$PATH
to envsubst
, then it expands only those variables that are referenced in that argument.
So one way could be to pass it all the currently defined environment variables in that format. With zsh
:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst $$(kj:$:)parameters[(R)*export*]
$parameters
is a special associative array that maps variable names to their type$parameters[(R)*export*]
expands to all the elements of the associative array whose value containsexport
.- with the
k
parameter expansion flag, the key instead of the value is returned j:$:
joins those elements with$
in between, and we add one at the start.
With other shells, you can always revert to perl
to get that list:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"
Beware both disclose your environment variable names in the output of ps
.
Instead, you could also do the whole thing in perl
:
perl -pe 's(?$ENV$1//$&ge'
Beware it has the same limitations as envsubst
in that it won't expand things like $VAR:-x
and would expand $HOME
in things like $HOME
or $$HOME
which a shell wouldn't.
add a comment |
It you pass an argument like $USER$PATH
to envsubst
, then it expands only those variables that are referenced in that argument.
So one way could be to pass it all the currently defined environment variables in that format. With zsh
:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst $$(kj:$:)parameters[(R)*export*]
$parameters
is a special associative array that maps variable names to their type$parameters[(R)*export*]
expands to all the elements of the associative array whose value containsexport
.- with the
k
parameter expansion flag, the key instead of the value is returned j:$:
joins those elements with$
in between, and we add one at the start.
With other shells, you can always revert to perl
to get that list:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"
Beware both disclose your environment variable names in the output of ps
.
Instead, you could also do the whole thing in perl
:
perl -pe 's(?$ENV$1//$&ge'
Beware it has the same limitations as envsubst
in that it won't expand things like $VAR:-x
and would expand $HOME
in things like $HOME
or $$HOME
which a shell wouldn't.
add a comment |
It you pass an argument like $USER$PATH
to envsubst
, then it expands only those variables that are referenced in that argument.
So one way could be to pass it all the currently defined environment variables in that format. With zsh
:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst $$(kj:$:)parameters[(R)*export*]
$parameters
is a special associative array that maps variable names to their type$parameters[(R)*export*]
expands to all the elements of the associative array whose value containsexport
.- with the
k
parameter expansion flag, the key instead of the value is returned j:$:
joins those elements with$
in between, and we add one at the start.
With other shells, you can always revert to perl
to get that list:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"
Beware both disclose your environment variable names in the output of ps
.
Instead, you could also do the whole thing in perl
:
perl -pe 's(?$ENV$1//$&ge'
Beware it has the same limitations as envsubst
in that it won't expand things like $VAR:-x
and would expand $HOME
in things like $HOME
or $$HOME
which a shell wouldn't.
It you pass an argument like $USER$PATH
to envsubst
, then it expands only those variables that are referenced in that argument.
So one way could be to pass it all the currently defined environment variables in that format. With zsh
:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst $$(kj:$:)parameters[(R)*export*]
$parameters
is a special associative array that maps variable names to their type$parameters[(R)*export*]
expands to all the elements of the associative array whose value containsexport
.- with the
k
parameter expansion flag, the key instead of the value is returned j:$:
joins those elements with$
in between, and we add one at the start.
With other shells, you can always revert to perl
to get that list:
echo 'Hello $USER $USER $UNDEFINED_VARIABLE' |
envsubst "$(perl -e 'print "$$_" for grep /^[_a-zA-Z]w*$/, keys %ENV')"
Beware both disclose your environment variable names in the output of ps
.
Instead, you could also do the whole thing in perl
:
perl -pe 's(?$ENV$1//$&ge'
Beware it has the same limitations as envsubst
in that it won't expand things like $VAR:-x
and would expand $HOME
in things like $HOME
or $$HOME
which a shell wouldn't.
edited Jan 6 at 9:41
answered Jan 6 at 9:11
Stéphane ChazelasStéphane Chazelas
301k55565917
301k55565917
add a comment |
add a comment |
You can use env
to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)
echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"
(The output of env
lists the values of the variables as well, but envsubst
also wants to see a leading $
, so we can't just use cut -d= -f1
on its own, unfortunately. You could use a single sed
to do cut
's job as well, see previous revision, but I prefer the clarity of cut
over a tiny performance gain.)
That assumes none of the env variables names or values contain newline characters (and names don't contain$
characters)
– Stéphane Chazelas
Jan 6 at 9:17
add a comment |
You can use env
to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)
echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"
(The output of env
lists the values of the variables as well, but envsubst
also wants to see a leading $
, so we can't just use cut -d= -f1
on its own, unfortunately. You could use a single sed
to do cut
's job as well, see previous revision, but I prefer the clarity of cut
over a tiny performance gain.)
That assumes none of the env variables names or values contain newline characters (and names don't contain$
characters)
– Stéphane Chazelas
Jan 6 at 9:17
add a comment |
You can use env
to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)
echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"
(The output of env
lists the values of the variables as well, but envsubst
also wants to see a leading $
, so we can't just use cut -d= -f1
on its own, unfortunately. You could use a single sed
to do cut
's job as well, see previous revision, but I prefer the clarity of cut
over a tiny performance gain.)
You can use env
to see all currently defined environment variables and then use that list to only replace those. (The man page isn't very clear on that, but see this answer for elucidation.)
echo 'Hello $USER $UNKNOWN' | envsubst "$(env | cut -d= -f1 | sed -e 's/^/$/')"
(The output of env
lists the values of the variables as well, but envsubst
also wants to see a leading $
, so we can't just use cut -d= -f1
on its own, unfortunately. You could use a single sed
to do cut
's job as well, see previous revision, but I prefer the clarity of cut
over a tiny performance gain.)
answered Jan 6 at 9:08
Ulrich SchwarzUlrich Schwarz
9,62512946
9,62512946
That assumes none of the env variables names or values contain newline characters (and names don't contain$
characters)
– Stéphane Chazelas
Jan 6 at 9:17
add a comment |
That assumes none of the env variables names or values contain newline characters (and names don't contain$
characters)
– Stéphane Chazelas
Jan 6 at 9:17
That assumes none of the env variables names or values contain newline characters (and names don't contain
$
characters)– Stéphane Chazelas
Jan 6 at 9:17
That assumes none of the env variables names or values contain newline characters (and names don't contain
$
characters)– Stéphane Chazelas
Jan 6 at 9:17
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%2f492772%2freplace-environment-variables-in-text-if-they-exist%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