How to Set Permanent and Temporary Linux Environment Variables for Bash and Csh
This guide explains how to configure both permanent and temporary environment variables on Linux for different shells (bash and csh), covering user‑specific and system‑wide settings, the required configuration files, and the exact commands to apply and verify the changes.
Linux environment variables can be defined as permanent (persisting across reboots) or temporary (valid only for the current shell session). The method varies with the shell type, so you must first identify the active shell using echo $SHELL. The article walks through four common scenarios: Bash permanent, Bash temporary, Csh permanent, and Csh temporary.
1. Bash – Permanent Variables (All Users / Single User)
Step 1: Run echo $SHELL to confirm the shell is /bin/bash.
Step 2 (single user): Add export VARIABLE=value to .profile in the user’s home directory (create the file if it does not exist).
Step 3 (all users): Add the same export VARIABLE=value line to /etc/profile for system‑wide effect.
Step 4: Apply the changes with source .profile (or re‑login for /etc/profile).
Step 5: Verify the variable with echo $VARIABLE.
$ echo $SHELL # shows /bin/bash
$ vi .profile # add: export ORACLE_HOME=/home/oracle/base
$ source .profile
$ echo $ORACLE_HOME # /home/oracle/base
$ sudo vi /etc/profile # add: export ORACLE_HOME=/home/oracle/base
$ source /etc/profile # or re‑login
$ echo $ORACLE_HOME2. Bash – Temporary Variables
Step 1: Confirm the shell with echo $SHELL.
Step 2: Set a variable for the current session using export VARIABLE=value. This variable disappears after the terminal is closed.
Step 3: Verify with echo $VARIABLE.
$ echo $SHELL
/bin/bash
$ export ORACLE_BASE="/opt/oracle/base"
$ echo $ORACLE_BASE
/opt/oracle/base3. Csh – Permanent Variables
Step 1: Check the shell with echo $SHELL (should be /usr/bin/csh).
Step 2 (single user): Add setenv VARIABLE value to .cshrc in the user’s home directory (create the file if missing).
Step 3: Apply the changes with source .cshrc.
Step 4: Verify with echo $VARIABLE.
$ echo $SHELL # shows /usr/bin/csh
$ vi .cshrc # add: setenv ORACLE_HOME /home/oracle/base
$ source .cshrc
$ echo $ORACLE_HOME # /home/oracle/base4. Csh – Temporary Variables
Step 1: Confirm the shell with echo $SHELL.
Step 2: Set a variable for the current session using the syntax set VARIABLE=value (or simply VARIABLE=value in csh). The variable is lost after the session ends.
Step 3: Verify with echo $VARIABLE.
$ echo $SHELL
/usr/bin/csh
$ ORACLE_BASE="/opt/oracle/base"
$ echo $ORACLE_BASE
/opt/oracle/baseBy following these steps, you can reliably manage environment variables on Linux regardless of the shell or scope required.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
