Linux on call · Chapter 18 of 30

Environment variables

By the end of this chapter you can pass settings to the programs you start, make your own tools work from any folder, and keep that for every new login. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Permissions and users
  • 4 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

Arjun's deploy.sh reads one setting, TAPRI_ENV, to know where to deploy. Bunty set it and the script still refused:

TAPRI_ENV=staging
./deploy.sh
echo $TAPRI_ENV
deploy.sh: TAPRI_ENV is not set. Did you export it?
staging

His shell knows staging; the script does not. And Arjun's handy chai-status only runs when you type its folder in front of it:

chai-status
bash: chai-status: command not found

Nothing you have learned so far crosses from your shell into a program it starts. You would have to edit the script to hard-code staging (and remember to change it back before production), and type tools/ from the one folder where that path works.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Deploy to staging, with TAPRI_ENV exported

    deploy.sh is a separate program, so it sees only exported variables. Give it TAPRI_ENV.

    1. Use export to set TAPRI_ENV to staging in your shell.

    2. Run ./deploy.sh in the chapter folder.

    3. Run cat deploy.log to see the deploy line.

    To pass: deploy.log has the line Deployed the ordering app to staging.

  2. Task 2

    Save your TAPRI_ variables in env.txt

    Save the exported variables whose names start with TAPRI_ in a file.

    1. Do task 1 first, so TAPRI_ENV is exported.

    2. Use env (or printenv) to list the exported variables.

    3. Keep only the lines that start with TAPRI_, and save them in env.txt, in the chapter folder.

    To pass: env.txt has the line TAPRI_ENV=staging, and you ran env (or printenv) in the chapter folder. A command with a typing mistake does not count, so type it again.

  3. Task 3

    Make chai-status work from any folder, in every new login

    Make chai-status work from any folder, typed as just its name, also after you log in again.

    1. Find the full path of the tools folder: /workspace/chapters/18-environment-variables/tools.

    2. Add a line to the end of ~/.profile that adds this folder to the end of PATH. Keep the folders PATH already has.

    3. Test it with a new login shell that starts in /, as the lesson shows.

    To pass: a new login shell, started in / with an empty environment, runs chai-status by its name. Only what ~/.profile sets counts, not an export in your shell.

  4. Task 4 · interview question

    Interview: deploy once to production, keep your shell on staging

    Interview question: how do you give a variable to one command only?

    1. Run ./deploy.sh with TAPRI_ENV=production written in front of it, on the same line.

    2. Run echo $TAPRI_ENV. Your shell still says staging.

    3. Run cat deploy.log. It has a production line now.

    To pass: deploy.log has the line Deployed the ordering app to production, and you ran TAPRI_ENV=production in front of ./deploy.sh in the chapter folder, and it worked. Never export production. If you did, run lab reset and do the chapter again.

What you will learn

  • Shell and environment variables: why a program sees some of your variables and not others, and export.
  • env and printenv: list what your programs will get.
  • A variable for one command: set it in front of the command.
  • PATH and ~/.profile: where the shell looks for commands, for this shell and for every login.

More in Permissions and users

Build "Environment variables" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.