You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bash/终端导出多行环境变量(如RSA私钥)

Fixing RSA Private Key as Environment Variable for github-backup (No Fancy Scripts Needed!)

I totally get your frustration—dealing with newline characters in environment variables is a classic pain point, especially when you're just trying to get an app up and running. Let's skip the complex scripts and use simple, terminal-friendly solutions that even beginners can follow.

Why Your Original Command Failed

First, quick context: When you pasted the private key directly into export, the actual newline characters in the key told your shell, "Okay, this command is done—now run whatever comes next." That's why you got the -----END RSA PRIVATE KEY-----': not a valid identifier error: the shell tried to treat the end of the key as a new command, which it wasn't.

Solution 1: One-Liner to Export the Key Immediately

Use this single terminal command to read your private key file, convert its newlines to escaped \n characters (which the shell can safely store in an environment variable), and export it:

export PRIVATE_KEY="$(cat ./gitbu.2018-03-23.private-key.pem | tr '\n' '\\n')"

How it works:

  • cat ./gitbu.2018-03-23.private-key.pem: Reads your private key file.
  • tr '\n' '\\n': Replaces every real newline (\n) with an escaped newline (\\n), so the shell doesn't interpret them as command breaks.
  • The entire output is wrapped in double quotes, ensuring it's treated as a single value for the environment variable.

To verify it worked, run echo "$PRIVATE_KEY"—you should see your full private key, formatted correctly.

Solution 2: Even Simpler (If Your App Accepts It)

Some tools accept private keys with spaces instead of newlines. If github-backup supports this, use this shorter command:

export PRIVATE_KEY="$(cat ./gitbu.2018-03-23.private-key.pem | tr '\n' ' ')"

This just replaces all newlines with spaces, creating a single-line key that's easy to export. Test it with your app first to make sure it works!

Solution 3: Make the Environment Variable Permanent

If you don't want to run the export command every time you open a terminal, add the key to your shell's configuration file:

  1. Run this command to append the escaped key to your .bashrc (use .zshrc if you use Zsh):
echo "export PRIVATE_KEY=\"$(cat ./gitbu.2018-03-23.private-key.pem | tr '\n' '\\n')\"" >> ~/.bashrc
  1. Apply the changes immediately with:
source ~/.bashrc

Now every time you open a terminal, PRIVATE_KEY will be available automatically.

内容的提问来源于stack exchange,提问作者nelsonic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:58:39