Skip to main content

Run a command with the subset of the current environment. env swiss army knife

Project description

env-subset -- Run a command with a subset of the environment

@readwithai - X - blog - machine-aided reading - 📖⚡️🖋️

env-subset is a swiss army knife for manipulating the environment used to run a command, written because env did not have the options I wanted and the other ways of doing things was unwieldy

Motivation

Programs change their behaviour based on the environment. It is therefore natural to want to be able to control the environment to control the program for debug and standard use. There are some tools for this - namely env and printenv but they are a little cumbersome.

This program is effectively "env with a lot of flags" to give you more control over the environment. This program is also more consistent than env and provides documentation via --help making it easier to install.

It is also released as open source code and can be installed with one (or two) lines on most systems.

Alternatives and prior work

You can hack up something quite similar to this use env, printenv, grep, bash -c etc. But this requires quite a lot of typing and is error prone.

If you want to change a single environment variable you can use e.g. env VAR=value printenv, bash and zsh can do this with VAR=value printenv. If you want to run without an environemt env -i printenv. If you want to run with a fixed environment you can use env -i env VAR=Value printenv. If you want to read environment variables from a file you can use env -i bash -c "source $file; printenv"

This is vaguely related to the idea of dotenv and python-dotenv.

For complete control, you can write your own program in python using the env option of subprocess - like this tool does.

Installation

You can install env-subset using pipx:

pipx install env-subset

Usage

Run ls is just the LANGUAGE environment. You can also use --variable

env-subset -e LANGUAGE ls

Run ls with both LANG and LANGUAGE

env-subset -e LANGUAGE ls

Run xterm with all the environment variables starting with X. This uses a regular expression. You can also use -r or --regexp

env-subset -e '/^X/' xterm

Run ls without the LANGUAGE enviornment variable - but with everything else. You can also use --exclude

env-subset -x -e LANGUAGE ls

Run a command with flags.

env-subset -e LANGUAGE -- python -m pdb

Set language to German:

env-subset -e LANGUAGE=de -e LANG=de -- python -m pdb

Read settings from a file. This can be generated with printenv but has some additional features. You can write the variable name along to take the variable from the outer scope. Write regular expressions of the form ^ to write a regular expression or // for a regular expression.

env-subset -f environment.env printenv

About me

I am @readwithai. I create tools for reading, research and agency sometimes using the markdown editor Obsidian.

I also create a stream of tools that are related to carrying out my work.

I write about lots of things - including tools like this - on X. My blog is more about reading and research and agency.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

env-subset-1.0.1.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file env-subset-1.0.1.tar.gz.

File metadata

  • Download URL: env-subset-1.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for env-subset-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e8847764acfa657d4055c502401fb25a4e15c9ce7afeeb24d545d43989b12fbe
MD5 b32a06a0c2c5fd5ee55e2850d0b6f386
BLAKE2b-256 bc1acb7a5953d81ea71f9cd669ae84584a748b576559f348a3eb746ccbde8f2f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page