Technology Encyclopedia Home >How to set environment variables on Fedora?

How to set environment variables on Fedora?

To set environment variables on Fedora, you can use several methods depending on whether you want the variable to be temporary or permanent.

Temporary Environment Variables:
You can set a temporary environment variable by using the export command in the terminal. This variable will only last for the duration of your current terminal session.

Example:

export MY_VARIABLE="Hello World"

After running this command, you can access MY_VARIABLE in the same terminal session with echo $MY_VARIABLE.

Permanent Environment Variables:
For permanent environment variables, you need to add the export command to your shell's configuration file. For Bash shell, this is typically ~/.bashrc or ~/.bash_profile.

Example:

  1. Open the file with a text editor, for example:
    nano ~/.bashrc
    
  2. Add the following line to set the environment variable permanently:
    export MY_VARIABLE="Hello World"
    
  3. Save the file and exit the editor.
  4. Apply the changes by sourcing the file:
    source ~/.bashrc
    

Now, MY_VARIABLE will be available in all new terminal sessions.

Using Systemd Environment Variables:
If you are working with systemd services, you might need to set environment variables in a systemd service file.

Example:

  1. Edit the service file, for example:
    sudo nano /etc/systemd/system/my_service.service
    
  2. Add an Environment line within the [Service] section:
    [Service]
    Environment=MY_VARIABLE="Hello World"
    
  3. Save the file and exit the editor.
  4. Restart the service to apply changes:
    sudo systemctl restart my_service
    

For managing environment variables in a cloud environment, consider using services like Tencent Cloud's Cloud Functions or Container Service, which provide mechanisms to set and manage environment variables for your applications.