How can I set an environment variable which contains newline characters?

notfreshprince

I'm trying to set an RSA key as an environment variable which, as a text file, contains newline characters.

Whenever I attempt to read from the file and pass it into an environment variable, it will just stop at the newline character on the first line. How can I prevent this?

Stéphane Chazelas

Note that except in zsh, shell variables cannot store arbitrary sequences of bytes. Variables in all other shells can't contain the NUL byte. And with the yash, they can't contain bytes not forming valid characters.

For files that don't contain NUL bytes, in POSIX-like shells, you can do:

var=$(cat file; echo .); var=${var%.}

We add a .\n and strip the trailing . to work around the fact that $(...) strips all trailing newline characters.

The above would also work in zsh for files that contain NULs though in zsh you could also use the $mapfile special associative array:

zmodload zsh/mapfile
var=$mapfile[file]

In zsh or bash, you can also use:

{ IFS= read -rd '' var || :; } < file

That reads up to the first NUL byte. It will return a non-zero exit status unless a NUL byte is found. We use the command group here to be able to at least tell the errors when opening the file, but we won't be able to detect read errors via the exit status.

Remember to quote that variable when passed to other commands. Newline is in the default value of $IFS, so would cause the variable content to be split when left unquoted in list contexts in POSIX-like shells other than zsh (not to mention the other problems with other characters of $IFS or wildcards).

So:

printf %s "$var"

for instance (not printf %s $var, certainly not echo $var which would add echo's problems in addition to the split+glob ones).


With non-POSIX shells:

Bourne shell:

The bourne shell did not support the $(...) form nor the ${var%pattern} operator, so it can be quite hard to achieve there. One approach is to use eval and quoting:

eval "var='`printf \\' | cat file - | awk -v RS=\\' -v ORS= -v b='\\\\' '
  NR > 1 {print RS b RS RS}; {print}; END {print RS}'`"

With (t)csh, it's even worse, see there.

With rc, you can use the ``(separator){...} form of command substitution with an empty separator list:

var = ``(){cat file}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I pass a header to curl that contains embedded newline characters?

How can I call a method of a variable, which contains in a namespace?

How can I access which environment is set in CodeIgniter?

How can I set runtime variable for docker compose environment variable

How can I check if an environment variable is set in Node.js?

How can I set the helm tiller namespace via an environment variable?

How can I set up the python environment variable on Windows 7?

How can I set .bash_profile environment variable in Dockerfile?

How can I set an environment variable as gulp task?

How can I set up an environment variable to work with TCC?

How can I consistently set an environment variable for a single program?

How can I set a runtime environment variable based on the value of another

How can I set the environment variable in request header for curl?

How can I set environment variable for another user account in Windows?

How can I parse a JSON data which contains special characters on property name?

How can I extract a part(number) of a string which contains special characters

How can I do a regex match string in a string which contains some characters or not?

How can I insert a newline after a certain amount of characters

How can I read in characters with getchar() without the newline?

How do I set an environment variable on Windows 10, which was generated from GitHub?

How can I check if a string contains only a particular set of special characters in Javascript?

How can I check if a string contains a given set of characters (disregarding order) using Regex?

How can I make a regular expression that contains special characters, like word boundaries, with a variable?

How can I pass environment variable to project which run on EMR Serverless?

How do I set an environment variable?

How to set a variable which contains other variables that will change in Bash?

How to set variable values based on which column number contains a string

How can I remove a variable that contains a string?

How can I set the newline separator for Doctrine-generated code?