CodeTheRobot

Ubuntu Linux for Robotics · Lesson 09 of 96

Environment variables: PATH, .bashrc and .bash_profile

Variables, export, PATH lookup, startup files and sourcing ROS from .bashrc.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain environment variables: path, .bashrc and .bash_profile in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

An environment variable is a named value that programs can read, such as HOME, USER or PATH. export makes a variable visible to programs you start. PATH is a list of folders separated by colons: when you type a command the shell searches those folders in order.

Changes made in a terminal last only for that terminal. To make them permanent put them in ~/.bashrc (read by every interactive terminal); ~/.bash_profile or ~/.profile are read at login. After editing, run source ~/.bashrc. Do not source ROS in .bashrc without understanding what it changes: it affects every new terminal.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the environment variables: path, .bashrc and .bash_profile commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

echo $PATH
# Print the folders searched for commands.
export ROBOT_NAME=arm1
# Create a variable for programs started from this terminal.
echo $ROBOT_NAME
# Read it back.
export PATH="$HOME/bin:$PATH"
# Put your own bin folder first in the search order.
echo 'export ROBOT_NAME=arm1' >> ~/.bashrc
# Make the variable permanent for future terminals.
source ~/.bashrc
# Reload the file in the current terminal.
printenv | sort | less
# Browse every variable (press q to quit).

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Build and run

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

sourcecompilerprogramBUILD AND RUN

Demo for Environment variables: PATH, .bashrc and .bash_profile. Expected result: arm1 /tmp/bin

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: export ROBOT_NAME=arm1 and print it
# TODO: put /tmp/bin at the front of PATH and print the first PATH entry with cut

Expected output

arm1
/tmp/bin
Stuck? Show the full solution (try it yourself first)
export ROBOT_NAME=arm1
echo "$ROBOT_NAME"
PATH="/tmp/bin:$PATH"
echo "$PATH" | cut -d: -f1

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Create ~/bin with a script hello.sh, add ~/bin to PATH in ~/.bashrc, open a new terminal and run hello.sh from any folder. Explain what would happen if ~/bin were placed after /usr/bin.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.