/printenv

A python script that displays operating system environment variable names and values in name order.

Primary LanguagePythonApache License 2.0Apache-2.0

Environment Variable Print Command

A python script that pretty-prints operating system environment variable names and values in name order on Windows, Linux and MacOS X systems.

Features

  • Name-sorted, pretty-printed version of the Unix printenv or env and Windows set command outputs.
  • When available, ANSI color codes are used to highlight names and values.
  • Command line options provide a convenient, formatted alternative to piping output to grep-like commands.
  • Variables containing multiple values may be printed on multiple lines, one per value, by providing the value separator string as a command line option.
  • Unformatted output option mimics OS-native environment variable vieiwing commands.

Supported Operating Systems

This script should run on all devices that support Python 2.7 or 3.x interpreters. It has been tested on Windows, Linux and MacOS X systems.

Installation and Prerequisites

You will need a supported Python interpreter installed on your target device before running this script.

Installing the script is as simple as downloading a copy of the printenv.py script and making sure that it is executable. Unix users can do this using curl and chmod, for example:

user@host ~ $ curl https://raw.githubusercontent.com/moebius-rex/printenv/master/printenv.py > printenv.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6940  100  6940    0     0  11875      0 --:--:-- --:--:-- --:--:-- 11863
user@host ~ $ chmod +x printenv.py

Windows users may be best served by simply copying and pasting the raw file contents. Windows scripts are executable by default.

Testing

The software has had some testing on Windows, Linux and MacOS X devices. There are no unit tests but they could be furnished upon request.

Usage Examples

All usage examples below show ANSI color terminal output. For the sake of brevity, all output is simulated.

1. Print all environment variables:

user@host ~ $ ./printenv.py
    DISPLAY :0
       LANG en_US.UTF-8
   LANGUAGE en_US
       PATH /home/user/bin:/usr/local/bin:/usr/bin:/bin
        PWD /home/user
      SHELL /bin/bash
      SHLVL 1
       TERM xterm-256color
       USER user
VTE_VERSION 4205
          _ ./printenv.py

2. Print variables containing colon-separated values on multiple lines, variable name on all lines:

user@host ~ $ ./printenv.py -s
    DISPLAY :0
       LANG en_US.UTF-8
   LANGUAGE en_US
       PATH /home/user/bin
       PATH /usr/local/bin
       PATH /usr/bin
       PATH /bin
        PWD /home/user
      SHELL /bin/bash
      SHLVL 1
       TERM xterm-256color
       USER user
VTE_VERSION 4205
          _ ./printenv.py

3. Print variables containing colon-separated values on multiple lines, variable name on first line only:

user@host ~ $ ./printenv.py -ns
    DISPLAY :0
       LANG en_US.UTF-8
   LANGUAGE en_US
       PATH /home/user/bin
            /usr/local/bin
            /usr/bin
            /bin
        PWD /home/user
      SHELL /bin/bash
      SHLVL 1
       TERM xterm-256color
       USER user
VTE_VERSION 4205
          _ ./printenv.py

4. Print only variables whose name or value contains a user-defined character sequence:

user@host ~ $ ./printenv.py PATH -s
PATH /home/user/bin
PATH /usr/local/bin
PATH /usr/bin
PATH /bin

Use with the -i option to ignore the case of the supplied character sequence.

Character sequences may contain regular expression metacharacters, for example:

user@host ~ $ ./printenv.py U.*8 -s
LANG en_US.UTF-8

On Unix systems, similar results can be achieved by piping the results to commands like grep and awk.

Print help:

Use the -h or --help option to print instructions for using the command:

user@host ~ $ ./printenv.py -h
usage: printenv.py [-h] [-c] [-e] [-i] [-n] [-r] [-s [sep]] [-u] [-w]
                   [char-sequence]

description:

  Display environment variable names and values in name order.

  Features:
    * Name-sorted, colorized (Unix only) version of Unix 'env'/'printenv'
      and Windows 'set' commands.
    * Command line options provide convenient, formatted alternative to
      piping output to grep-like commands.
    * Variables containing multiple values may be printed on multiple
      lines, one per value, by providing the value separator string as
      a command line option.

positional arguments:
  char-sequence         print only those variable names/values that contain
                        this character sequence

optional arguments:
  -h, --help            show this help message and exit
  -c, --clear           clear terminal before printing
  -e, --exact-match     print only those variable names/values that exactly
                        match the character sequence provided
  -i, --ignore          ignore case in character sequence if supplied
  -n, --no-name-repeat  when used with -s option, suppress printing of
                        variable name on all but first line of multiple value
                        variables
  -r, --reset           reset terminal before printing
  -s [sep], --split [sep]
                        split multiple value variables by supplied 'sep'
                        (separator, default ':') string and print one value
                        per line
  -u, --unformatted     sort by name but disable all output formatting to
                        produce output similar to that of native operating
                        system commands
  -w, --wait            prompt user to exit script (useful when launching a
                        terminal window to run this command)

Authors

Licensing and distribution

This project is licensed under the MIT License — see LICENSE for details.

Acknowledgments

  • Bill Joy for his original 1979 implementation of printenv for BSD
  • Tracy and Paul for their support