Environment Path

Environment Path Notes - unix

Environment Variables

List environment variables:

printenv

List specific environment variable:

printenv HOME

Reference environment variable:

echo $HOME

Set environment variable:

MYVAR= "this is my var"
echo $MYVAR

Setting an environment variable in this way allows us to pass them as settings to scripts.

A (overly) simpl(ified) makefile:

all:
    gcc $MYARGS main.cpp

Default invocation (MYARGS not defined):

make

Result:

gcc main.cpp

Debugging invocation:

MYARGS= "-Wall -g" make

Result:

gcc -Wall -g main.cpp

main.cpp compiles to a.out with verbose warnings and symbols for debugging (gdb).

Release invocation:

MYARGS= "-Wall -DNDEBUG" make

Result”

gcc -Wall -O2 -DNDEBUG main.cpp

main.cpp compiles to a.out with verbose warnings, optimization, and NDEBUG defined.


#include <iostream>

int main( int argc, char* argv[] ) {
#ifndef NDEBUG
    std::cout << "DEBUG:\tint main() {" << "\n";
#endif

    std::cout << "Hello World!" << "\n";

#ifndef NDEBUG
    std::cout << "DEBUG:\t} int main()" << "\n";
#endif

    return 0;
}

Result:

With debugging:

DEBUG:  int main() {
Hello World!
DEBUG:  } int main

With debugging stripped:

Hello World!

To make a defined environment variable available elsewhere, it needs exported.

Export environment variable:

export MYVAR= "this is my var"

To define a persistent environment variable, it needs to be set in shell profile.

See Shell Invocation Overview.

Persistent environment variables can be defined in ~/.bashrc.