Catalog My learning Blog
Linux Command Line Tutorial for Developers · Phase 1 · Lesson 7 · 3 min read

Linux Environment Variables and .bashrc

On this page
  1. What environment variables are for
  2. Shell variables vs environment variables: export
  3. How PATH works
  4. .bashrc vs .bash_profile vs .profile: which runs when
  5. Works in my terminal, fails in cron
  6. Secrets in environment variables
  7. Check yourself

What environment variables are for

Programs read configuration from their environment: PATH to find commands, HOME, LANG, and application settings like DATABASE_URL or PORT. Every process gets a copy of its parent's environment when it starts.

Shell variables vs environment variables: export

Code
FOO=bar
bash -c 'echo "child sees FOO=[$FOO]"'
export FOO
bash -c 'echo "child sees FOO=[$FOO]"'
Code
child sees FOO=[]
child sees FOO=[bar]

A plain assignment creates a shell variable, visible only in this shell. export puts it in the environment, copied to child processes. Changes in a child never flow back to the parent.

Set a variable for one command only: PORT=9090 ./server.

How PATH works

PATH is a colon-separated list of directories searched for commands, first match wins (Day 1):

Code
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

Add a directory by prepending or appending:

Shell
export PATH="$HOME/.local/bin:$PATH"

Prepending means your version wins over the system's; appending means the system's wins. Never put . (the current directory) in PATH.

.bashrc vs .bash_profile vs .profile: which runs when

For bash:

SessionReads
Login shell (SSH login, su -, a console)/etc/profile, then the first of ~/.bash_profile, ~/.bash_login, ~/.profile
Interactive non-login (a new terminal tab, bash)~/.bashrc
Non-interactive (scripts, cron, systemd)Neither, unless BASH_ENV points somewhere

Ubuntu's default ~/.profile sources ~/.bashrc, so most people put everything in ~/.bashrc. The catch is the last row.

Login shells read profile files, interactive shells read .bashrc, and non-interactive contexts like cron and systemd read neither, so they get a different environment from your terminal.
Login shells read profile files, interactive shells read .bashrc, and non-interactive contexts like cron and systemd read neither, so they get a different environment from your terminal.

Works in my terminal, fails in cron

Alice installs a tool in ~/.local/bin and adds that directory to PATH in ~/.bashrc. In her terminal:

Code
which make-report
Code
/home/alice/.local/bin/make-report

She schedules it with cron (Day 20):

Code
* * * * * make-report >> /home/alice/report.log 2>&1

A minute later, report.log contains:

Code
/bin/sh: 1: make-report: not found

Cron runs commands with /bin/sh, non-interactively, so ~/.bashrc is never read, and its PATH (on this Ubuntu version, the system default from /etc/environment) lacks ~/.local/bin. With the full path, $HOME/.local/bin/make-report, the log shows report generated.

The same applies to systemd services, Docker containers and CI jobs: don't rely on interactive shell configuration. Use absolute paths, or set the environment yourself (Environment= in a unit file, PATH= at the top of a crontab).

Secrets in environment variables

Environment variables are a common way to pass secrets to applications (twelve-factor style), with caveats: they're inherited by every child process, readable by the same user in /proc/<pid>/environ, and easily printed in debug output or crash reports. Load them from a protected file (systemd EnvironmentFile= with mode 600, Day 19) or a secrets manager, and never commit .env files.

Check yourself

Plain assignments create shell variables that live only in the current shell. export marks them for the environment, and only environment variables are copied into child processes when they start.

Cron runs commands with a minimal non-interactive sh that doesn't read ~/.bashrc, so the PATH addition for ~/.local/bin wasn't applied. The fix is to set PATH in the crontab or call the script by its absolute path.

Directories earlier in PATH are searched first, so prepending lets your programs override system ones; appending gives system programs priority.

They can leak into child processes, crash reports and /proc/<pid>/environ, and get printed by debugging tools. Secret files or a secret manager are safer.

.bash_profile runs for login shells, such as an SSH session, and .bashrc for interactive non-login shells, such as a new terminal tab. Many setups source .bashrc from .bash_profile so both get the same settings.