Safest way to store environment variable value in a fileWhy is printf better than echo?Bash shell functions and understanding shell shockLooping through lines in file using bash and passing to variable. Resulting variable is not the same value as file, why?Substitute variable in value of chpst loaded environment variablebash script that incorporates content from a file as part of a commandNeed to capture stdout and modify variable by reference from function in bashSED or AWK?: Add character to each line after found patternWhy can't I print a variable I can see in the output of env?sed - calling a variable from a file with multilineBASH: declare a local function in remote host with interactive shellSet shell variable then invoke subprocess fails
Compelling story with the world as a villain
Showing that the limit of non-eigenvector goes to infinity
Prove your innocence
Non-visual Computers - thoughts?
Is there any way to keep a player from killing an NPC?
How can I unambiguously ask for a new user's "Display Name"?
"Sorry to bother you" in an email?
Heyacrazy: No Diagonals
Why is there a difference between predicting on Validation set and Test set?
Did anyone try to find the little box that held Professor Moriarty and his wife after the crash?
Why do all fields in a QFT transform like *irreducible* representations of some group?
Numbers Decrease while Letters Increase
'Us students' - Does this apposition need a comma?
How do I make my image comply with the requirements of this photography competition?
If an earthquake can destroy buildings why it cant kill us according to physics?
How do we calculate energy of food?
Is gzip atomic?
Where can/should I, as a high schooler, publish a paper regarding the derivation of a formula?
How to know which loss function is suitable for image classification?
Why are non-collision-resistant hash functions considered insecure for signing self-generated information
Are modern clipless shoes and pedals that much better than toe clips and straps?
Transposing from C to Cm?
New Math Formula?
Why in most German places is the church the tallest building?
Safest way to store environment variable value in a file
Why is printf better than echo?Bash shell functions and understanding shell shockLooping through lines in file using bash and passing to variable. Resulting variable is not the same value as file, why?Substitute variable in value of chpst loaded environment variablebash script that incorporates content from a file as part of a commandNeed to capture stdout and modify variable by reference from function in bashSED or AWK?: Add character to each line after found patternWhy can't I print a variable I can see in the output of env?sed - calling a variable from a file with multilineBASH: declare a local function in remote host with interactive shellSet shell variable then invoke subprocess fails
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am using bash
and most interested in an answer for bash
, but you can answer about how to do it in other shells if you want.
I have an environment variable, KEY
, and I want to store the value of that environment variable in a a file. Currently I am doing
echo "$KEY" > ./key.pem
In practice this seems to be OK, but in theory this breaks when KEY=-n
.
$ export KEY=-n
$ echo BEGIN "$KEY" END
BEGIN -n END
$ echo "$KEY" END
END$
So, is there a better way to store the value of a single environment variable in a file? (Note that export
and declare
will include the name of the variable in their outputs, which is no good for me.)
bash scripting
|
show 4 more comments
I am using bash
and most interested in an answer for bash
, but you can answer about how to do it in other shells if you want.
I have an environment variable, KEY
, and I want to store the value of that environment variable in a a file. Currently I am doing
echo "$KEY" > ./key.pem
In practice this seems to be OK, but in theory this breaks when KEY=-n
.
$ export KEY=-n
$ echo BEGIN "$KEY" END
BEGIN -n END
$ echo "$KEY" END
END$
So, is there a better way to store the value of a single environment variable in a file? (Note that export
and declare
will include the name of the variable in their outputs, which is no good for me.)
bash scripting
What will use the file? Will it be used to set the value again in a shell script, or by some other tool?
– muru
Aug 12 at 17:38
1
@drewbennecho -- "$KEY"
would not help. It would output-- -n
.
– Kusalananda♦
Aug 12 at 17:45
1
@Kusalananda, I'm a bit disappointed that all my shells consistently output-- -n
– ilkkachu
Aug 12 at 17:53
1
This is one of the reasons whyprintf
is always better thanecho
when you don't have complete control over what input you want to print.
– terdon♦
Aug 12 at 18:12
1
@ilkkachu, therc
clone for Unix and its es/akanga derivatives supportecho --
. Early versions ofzsh
did as well, but the end-of-option delimiter later changed to-
for consistency with Bourne/Korn shell and builtins I suppose.
– Stéphane Chazelas
Aug 13 at 7:22
|
show 4 more comments
I am using bash
and most interested in an answer for bash
, but you can answer about how to do it in other shells if you want.
I have an environment variable, KEY
, and I want to store the value of that environment variable in a a file. Currently I am doing
echo "$KEY" > ./key.pem
In practice this seems to be OK, but in theory this breaks when KEY=-n
.
$ export KEY=-n
$ echo BEGIN "$KEY" END
BEGIN -n END
$ echo "$KEY" END
END$
So, is there a better way to store the value of a single environment variable in a file? (Note that export
and declare
will include the name of the variable in their outputs, which is no good for me.)
bash scripting
I am using bash
and most interested in an answer for bash
, but you can answer about how to do it in other shells if you want.
I have an environment variable, KEY
, and I want to store the value of that environment variable in a a file. Currently I am doing
echo "$KEY" > ./key.pem
In practice this seems to be OK, but in theory this breaks when KEY=-n
.
$ export KEY=-n
$ echo BEGIN "$KEY" END
BEGIN -n END
$ echo "$KEY" END
END$
So, is there a better way to store the value of a single environment variable in a file? (Note that export
and declare
will include the name of the variable in their outputs, which is no good for me.)
bash scripting
bash scripting
asked Aug 12 at 17:05
Old ProOld Pro
6322 gold badges6 silver badges15 bronze badges
6322 gold badges6 silver badges15 bronze badges
What will use the file? Will it be used to set the value again in a shell script, or by some other tool?
– muru
Aug 12 at 17:38
1
@drewbennecho -- "$KEY"
would not help. It would output-- -n
.
– Kusalananda♦
Aug 12 at 17:45
1
@Kusalananda, I'm a bit disappointed that all my shells consistently output-- -n
– ilkkachu
Aug 12 at 17:53
1
This is one of the reasons whyprintf
is always better thanecho
when you don't have complete control over what input you want to print.
– terdon♦
Aug 12 at 18:12
1
@ilkkachu, therc
clone for Unix and its es/akanga derivatives supportecho --
. Early versions ofzsh
did as well, but the end-of-option delimiter later changed to-
for consistency with Bourne/Korn shell and builtins I suppose.
– Stéphane Chazelas
Aug 13 at 7:22
|
show 4 more comments
What will use the file? Will it be used to set the value again in a shell script, or by some other tool?
– muru
Aug 12 at 17:38
1
@drewbennecho -- "$KEY"
would not help. It would output-- -n
.
– Kusalananda♦
Aug 12 at 17:45
1
@Kusalananda, I'm a bit disappointed that all my shells consistently output-- -n
– ilkkachu
Aug 12 at 17:53
1
This is one of the reasons whyprintf
is always better thanecho
when you don't have complete control over what input you want to print.
– terdon♦
Aug 12 at 18:12
1
@ilkkachu, therc
clone for Unix and its es/akanga derivatives supportecho --
. Early versions ofzsh
did as well, but the end-of-option delimiter later changed to-
for consistency with Bourne/Korn shell and builtins I suppose.
– Stéphane Chazelas
Aug 13 at 7:22
What will use the file? Will it be used to set the value again in a shell script, or by some other tool?
– muru
Aug 12 at 17:38
What will use the file? Will it be used to set the value again in a shell script, or by some other tool?
– muru
Aug 12 at 17:38
1
1
@drewbenn
echo -- "$KEY"
would not help. It would output -- -n
.– Kusalananda♦
Aug 12 at 17:45
@drewbenn
echo -- "$KEY"
would not help. It would output -- -n
.– Kusalananda♦
Aug 12 at 17:45
1
1
@Kusalananda, I'm a bit disappointed that all my shells consistently output
-- -n
– ilkkachu
Aug 12 at 17:53
@Kusalananda, I'm a bit disappointed that all my shells consistently output
-- -n
– ilkkachu
Aug 12 at 17:53
1
1
This is one of the reasons why
printf
is always better than echo
when you don't have complete control over what input you want to print.– terdon♦
Aug 12 at 18:12
This is one of the reasons why
printf
is always better than echo
when you don't have complete control over what input you want to print.– terdon♦
Aug 12 at 18:12
1
1
@ilkkachu, the
rc
clone for Unix and its es/akanga derivatives support echo --
. Early versions of zsh
did as well, but the end-of-option delimiter later changed to -
for consistency with Bourne/Korn shell and builtins I suppose.– Stéphane Chazelas
Aug 13 at 7:22
@ilkkachu, the
rc
clone for Unix and its es/akanga derivatives support echo --
. Early versions of zsh
did as well, but the end-of-option delimiter later changed to -
for consistency with Bourne/Korn shell and builtins I suppose.– Stéphane Chazelas
Aug 13 at 7:22
|
show 4 more comments
2 Answers
2
active
oldest
votes
If it's storing for the sake of reading it in later (in a bash
script), just use declare -p KEY
and then source the file to read it in again. If you just want to store the value, use printf '%sn' "$KEY"
as you would do when you output any variable data.
So,
printf '%sn' "$KEY" >key.pem
or
printf 'BEGIN %s ENDn' "$KEY" >key.pem
or whatever you need to output.
Your issue occurs since -n
is a valid option to echo
in bash
. The strings -e
and -E
(and combinations like -neEne
) would also cause issues in bash
, for the same reason. Depending on how bash
is built or the environment or options, backslash characters in arguments may also be a problem.
These issues and more are outlined in the following Q/A:
- Why is printf better than echo?
add a comment |
Many systems have a printenv
command that outputs the contents of the given environment variable followed by one newline character to standard output (printenv
appeared in 3BSD in the late 70s):
printenv 'My Env Var' > file
Would store the content of the My Env Var
variable followed by NL into file
in all shells on those systems.
For environment variables that are mapped to shell variables (includes at least those whose name starts with an ASCII letter or underscore and are followed by 0 or more ASCII letters, digits or underscore, and are not otherwise special variables set by the shell), in Bourne-like shells (also works in fish
), you can do the same with:
printf '%sn' "$ENVVAR" > file
(though if the variable is unset, that will still store an empty line into file
)
In rc
-like shells (where all shell variables are mapped to environment variables):
printf '%sn' $ENVVAR > file
printf '%sn' $'My Env Var' > file
(same caveat as above)
In csh
-like shells:
printf '%sn' $ENVVAR:q > file
(if the variable is not set, it will fail and not overwrite file
)
Some implementations/versions of ksh
don't have printf
builtin. In those, you could also do:
print -r -- "$ENVVAR" > file
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%2f535214%2fsafest-way-to-store-environment-variable-value-in-a-file%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
If it's storing for the sake of reading it in later (in a bash
script), just use declare -p KEY
and then source the file to read it in again. If you just want to store the value, use printf '%sn' "$KEY"
as you would do when you output any variable data.
So,
printf '%sn' "$KEY" >key.pem
or
printf 'BEGIN %s ENDn' "$KEY" >key.pem
or whatever you need to output.
Your issue occurs since -n
is a valid option to echo
in bash
. The strings -e
and -E
(and combinations like -neEne
) would also cause issues in bash
, for the same reason. Depending on how bash
is built or the environment or options, backslash characters in arguments may also be a problem.
These issues and more are outlined in the following Q/A:
- Why is printf better than echo?
add a comment |
If it's storing for the sake of reading it in later (in a bash
script), just use declare -p KEY
and then source the file to read it in again. If you just want to store the value, use printf '%sn' "$KEY"
as you would do when you output any variable data.
So,
printf '%sn' "$KEY" >key.pem
or
printf 'BEGIN %s ENDn' "$KEY" >key.pem
or whatever you need to output.
Your issue occurs since -n
is a valid option to echo
in bash
. The strings -e
and -E
(and combinations like -neEne
) would also cause issues in bash
, for the same reason. Depending on how bash
is built or the environment or options, backslash characters in arguments may also be a problem.
These issues and more are outlined in the following Q/A:
- Why is printf better than echo?
add a comment |
If it's storing for the sake of reading it in later (in a bash
script), just use declare -p KEY
and then source the file to read it in again. If you just want to store the value, use printf '%sn' "$KEY"
as you would do when you output any variable data.
So,
printf '%sn' "$KEY" >key.pem
or
printf 'BEGIN %s ENDn' "$KEY" >key.pem
or whatever you need to output.
Your issue occurs since -n
is a valid option to echo
in bash
. The strings -e
and -E
(and combinations like -neEne
) would also cause issues in bash
, for the same reason. Depending on how bash
is built or the environment or options, backslash characters in arguments may also be a problem.
These issues and more are outlined in the following Q/A:
- Why is printf better than echo?
If it's storing for the sake of reading it in later (in a bash
script), just use declare -p KEY
and then source the file to read it in again. If you just want to store the value, use printf '%sn' "$KEY"
as you would do when you output any variable data.
So,
printf '%sn' "$KEY" >key.pem
or
printf 'BEGIN %s ENDn' "$KEY" >key.pem
or whatever you need to output.
Your issue occurs since -n
is a valid option to echo
in bash
. The strings -e
and -E
(and combinations like -neEne
) would also cause issues in bash
, for the same reason. Depending on how bash
is built or the environment or options, backslash characters in arguments may also be a problem.
These issues and more are outlined in the following Q/A:
- Why is printf better than echo?
edited Aug 12 at 20:29
answered Aug 12 at 17:16
Kusalananda♦Kusalananda
161k18 gold badges318 silver badges505 bronze badges
161k18 gold badges318 silver badges505 bronze badges
add a comment |
add a comment |
Many systems have a printenv
command that outputs the contents of the given environment variable followed by one newline character to standard output (printenv
appeared in 3BSD in the late 70s):
printenv 'My Env Var' > file
Would store the content of the My Env Var
variable followed by NL into file
in all shells on those systems.
For environment variables that are mapped to shell variables (includes at least those whose name starts with an ASCII letter or underscore and are followed by 0 or more ASCII letters, digits or underscore, and are not otherwise special variables set by the shell), in Bourne-like shells (also works in fish
), you can do the same with:
printf '%sn' "$ENVVAR" > file
(though if the variable is unset, that will still store an empty line into file
)
In rc
-like shells (where all shell variables are mapped to environment variables):
printf '%sn' $ENVVAR > file
printf '%sn' $'My Env Var' > file
(same caveat as above)
In csh
-like shells:
printf '%sn' $ENVVAR:q > file
(if the variable is not set, it will fail and not overwrite file
)
Some implementations/versions of ksh
don't have printf
builtin. In those, you could also do:
print -r -- "$ENVVAR" > file
add a comment |
Many systems have a printenv
command that outputs the contents of the given environment variable followed by one newline character to standard output (printenv
appeared in 3BSD in the late 70s):
printenv 'My Env Var' > file
Would store the content of the My Env Var
variable followed by NL into file
in all shells on those systems.
For environment variables that are mapped to shell variables (includes at least those whose name starts with an ASCII letter or underscore and are followed by 0 or more ASCII letters, digits or underscore, and are not otherwise special variables set by the shell), in Bourne-like shells (also works in fish
), you can do the same with:
printf '%sn' "$ENVVAR" > file
(though if the variable is unset, that will still store an empty line into file
)
In rc
-like shells (where all shell variables are mapped to environment variables):
printf '%sn' $ENVVAR > file
printf '%sn' $'My Env Var' > file
(same caveat as above)
In csh
-like shells:
printf '%sn' $ENVVAR:q > file
(if the variable is not set, it will fail and not overwrite file
)
Some implementations/versions of ksh
don't have printf
builtin. In those, you could also do:
print -r -- "$ENVVAR" > file
add a comment |
Many systems have a printenv
command that outputs the contents of the given environment variable followed by one newline character to standard output (printenv
appeared in 3BSD in the late 70s):
printenv 'My Env Var' > file
Would store the content of the My Env Var
variable followed by NL into file
in all shells on those systems.
For environment variables that are mapped to shell variables (includes at least those whose name starts with an ASCII letter or underscore and are followed by 0 or more ASCII letters, digits or underscore, and are not otherwise special variables set by the shell), in Bourne-like shells (also works in fish
), you can do the same with:
printf '%sn' "$ENVVAR" > file
(though if the variable is unset, that will still store an empty line into file
)
In rc
-like shells (where all shell variables are mapped to environment variables):
printf '%sn' $ENVVAR > file
printf '%sn' $'My Env Var' > file
(same caveat as above)
In csh
-like shells:
printf '%sn' $ENVVAR:q > file
(if the variable is not set, it will fail and not overwrite file
)
Some implementations/versions of ksh
don't have printf
builtin. In those, you could also do:
print -r -- "$ENVVAR" > file
Many systems have a printenv
command that outputs the contents of the given environment variable followed by one newline character to standard output (printenv
appeared in 3BSD in the late 70s):
printenv 'My Env Var' > file
Would store the content of the My Env Var
variable followed by NL into file
in all shells on those systems.
For environment variables that are mapped to shell variables (includes at least those whose name starts with an ASCII letter or underscore and are followed by 0 or more ASCII letters, digits or underscore, and are not otherwise special variables set by the shell), in Bourne-like shells (also works in fish
), you can do the same with:
printf '%sn' "$ENVVAR" > file
(though if the variable is unset, that will still store an empty line into file
)
In rc
-like shells (where all shell variables are mapped to environment variables):
printf '%sn' $ENVVAR > file
printf '%sn' $'My Env Var' > file
(same caveat as above)
In csh
-like shells:
printf '%sn' $ENVVAR:q > file
(if the variable is not set, it will fail and not overwrite file
)
Some implementations/versions of ksh
don't have printf
builtin. In those, you could also do:
print -r -- "$ENVVAR" > file
edited Aug 13 at 7:18
answered Aug 12 at 19:44
Stéphane ChazelasStéphane Chazelas
331k58 gold badges646 silver badges1016 bronze badges
331k58 gold badges646 silver badges1016 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%2f535214%2fsafest-way-to-store-environment-variable-value-in-a-file%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
What will use the file? Will it be used to set the value again in a shell script, or by some other tool?
– muru
Aug 12 at 17:38
1
@drewbenn
echo -- "$KEY"
would not help. It would output-- -n
.– Kusalananda♦
Aug 12 at 17:45
1
@Kusalananda, I'm a bit disappointed that all my shells consistently output
-- -n
– ilkkachu
Aug 12 at 17:53
1
This is one of the reasons why
printf
is always better thanecho
when you don't have complete control over what input you want to print.– terdon♦
Aug 12 at 18:12
1
@ilkkachu, the
rc
clone for Unix and its es/akanga derivatives supportecho --
. Early versions ofzsh
did as well, but the end-of-option delimiter later changed to-
for consistency with Bourne/Korn shell and builtins I suppose.– Stéphane Chazelas
Aug 13 at 7:22