Home > Software design >  printf prints commas to file
printf prints commas to file

Time:04-25

I'm having an environment variable that get's printed to a file which is then read by my program. When the variable gets written to the file it looks like this:

-----BEGIN
,RSA
,PRIVATE
,KEY-----
,MIIEowIBAAKCAQEAtxPgpPqD1cZdoTeOMvOnqp0NkkCqcMsn8V4j9KrFWpPxiweu
,H1r69S2ssmuqtleLVKk2kwgTn6x AvcqgTBLsjnfpPmD2mBKvTqCvaBT2VXdxGiA
,dlp  etc....

When it should look like this:

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtxPgpPqD1cZdoTeOMvOnqp0NkkCqcMsn8V4j9KrFWpPxiweu
H1r69S2ssmuqtleLVKk2kwgTn6x AvcqgTBLsjnfpPmD2mBKvTqCvaBT2VXdxGiA
dlpJMJAvCwBsDnDRilSRoNja4DpF26bHSQePwZF1/4OqnF6GtvGcPPPENiJkjxr/ etc...

My script command looks like this:

- printf '%s\n', $PRIVATE_KEY > $CI_PROJECT_DIR/private.pem

What am I doing wrong?

CodePudding user response:

printf '%s\n' "$PRIVATE_KEY" > "$CI_PROJECT_DIR"/private.pem

Quote "$PRIVATE_KEY" to prevent it from being split into separate words. As a rule of thumb, always quote variable expansions to prevent accidental mangling of their values. It's a good habit to get into.

Also, remove the trailing comma from '%s\n',. Shell script arguments are not comma-separated.

  • Related