Voltar ao ranking

hyperupcall/autoenv

Shell

Directory-based environments.

environmentcdshell-extensionshell-scriptsbashzshshellshell-scriptterminal
Crescimento de estrelas
Estrelas
6k
Forks
272
Crescimento semanal
Issues
11
2k4k6k
fev. de 2012nov. de 2016set. de 2021jul. de 2026
README

Autoenv: Directory-based Environments Build Status

Magic per-project shell environments.

What is it?

If a directory contains an .env file, it will automatically be executed when you cd into it. And, if a directory contains an .env.leave file (and AUTOENV_ENABLE_LEAVE is a non-empty string), the file will automatically be executed when cd'ing away from the directory that contains that file.

This is great for...

  • auto-activating virtualenvs
  • auto-deactivating virtualenvs
  • project-specific environment variables
  • making millions

You can also nest envs within each other. How awesome is that!?

When executing, autoenv, will walk up the directories until the mount point and execute all .env files beginning at the top.

Usage

Follow the white rabbit:

$ echo "echo 'whoa'" > ./project/.env
$ cd ./project
whoa

Mind blown GIF

Installation (automated)

Install with cURL:

curl -#fLo- 'https://raw.githubusercontent.com/hyperupcall/autoenv/main/scripts/install.sh' | sh

Install with wget:

wget --show-progress -o /dev/null -O- 'https://raw.githubusercontent.com/hyperupcall/autoenv/main/scripts/install.sh' | sh

If you encounter some variant of a "curl: command not found" or "wget: command not found" error, please install either cURL or wget (with your package manager) and try again.

Installation (manual)

When installing manually, you first install autoenv with either Homebrew, npm, or Git. Then, you run a command to ensure autoenv is loaded when you open a terminal (this command depends on your default shell).

Installation Method

Note that depending on your shell and operating system, you may need to write to .zprofile instead of .zshrc, or write to .bash_profile instead of .bashrc (or visa-versa).

Using Homebrew

Prefer this if you're running macOS. Homebrew must be installed.

Click to expand content

First, download the autoenv homebrew formulae:

$ brew install 'autoenv'

Then, execute one of the following to ensure autoenv is loaded when you open a terminal:

# For Zsh shell (on Linux or macOS)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"

# For Bash shell (on Linux)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> ~/.bashrc

# For Bash shell (on macOS)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> ~/.bash_profile

Using npm

Prefer this if you're running Linux or an unsupported version of macOS. npm must be installed (usually through NodeJS).

Click to expand content

First, download the @hyperupcall/autoenv npm package:

$ npm install -g '@hyperupcall/autoenv'

Then, execute one of the following to ensure autoenv is loaded when you open a terminal:

# For Zsh shell (on Linux or macOS)
$ printf '%s\n' "source $(npm root -g)/@hyperupcall/autoenv/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"

# For Bash shell (on Linux)
$ printf '%s\n' "source $(npm root -g)/@hyperupcall/autoenv/activate.sh" >> ~/.bashrc

# For Bash shell (on macOS)
$ printf '%s\n' "source $(npm root -g)/@hyperupcall/autoenv/activate.sh" >> ~/.bash_profile

Using Git

Use this if you cannot install with Homebrew or npm.

Click to expand content

First, clone this repository:

$ git clone 'https://github.com/hyperupcall/autoenv' ~/.autoenv

Then, execute one of the following to ensure autoenv is loaded when you open a terminal:

# For Zsh shell (on Linux or macOS)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"

# For Bash shell (on Linux)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> ~/.bashrc

# For Bash shell (on macOS)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> ~/.bash_profile

Configuration

Before sourceing activate.sh, you can set the following variables:

  • AUTOENV_AUTH_FILE: Files authorized to be sourced; defaults to ~/.autoenv_authorized if it exists, otherwise, ~/.local/state/autoenv/authorized_list
  • AUTOENV_NOTAUTH_FILE: Files not authorized to be sourced; defaults to ~/.autoenv_not_authorized if it exists, otherwise, ~/.local/state/autoenv/not_authorized_list
  • AUTOENV_ENV_FILENAME: Name of the .env file; defaults to .env
  • AUTOENV_LOWER_FIRST: Set this variable to a non-empty string to flip the order of .env files executed
  • AUTOENV_ENV_LEAVE_FILENAME: Name of the .env.leave file; defaults to .env.leave
  • AUTOENV_ENABLE_LEAVE: Set this to a non-empty string in order to enable source env when leaving
  • AUTOENV_ASSUME_YES: Set this variable to a non-empty string to silently authorize the initialization of new environments
  • AUTOENV_VIEWER: Program used to display env files prior to authorization; defaults to cat
  • AUTOENV_PRESERVE_CD: Set this variable to a non-empty string to prevent the cd builtin from being overridden (to active autoenv, you must invoke autoenv_init within a cd function of your own)

We recommend setting the following configuration variables:

AUTOENV_ENABLE_LEAVE=yes
AUTOENV_VIEWER=cat

These options are not set by default as to conform to the expectations of backwards-compatible behavior.

API

Inside the .env file, two environment variables can be accessed:

  • AUTOENV_CUR_FILE - The file being sourced
  • AUTOENV_CUR_DIR - Equivalent to dirname "$AUTOENV_CUR_FILE"

Shells

autoenv is tested on:

  • Bash
  • Zsh
  • Dash
  • Fish is supported by autoenv_fish
  • More to come

Disclaimer

Autoenv overrides cd (unless AUTOENV_PRESERVE_CD is set to a non-empty string). If you already do this, invoke autoenv_init within your custom cd after sourcing activate.sh.

If you define a cd alias, autoenv will (properly) show an error except when using Zsh. We recommend removing any aliases to cd! (unalias cd).

If you are using dash, autoenv will work. However, dash does not support builtin. As a result, in dash, autoenv invokes chdir instead of cd to prevent infinite loops.

Autoenv can be disabled via unset -f cd if you experience I/O issues with certain file systems, particularly those that are FUSE-based (such as smbnetfs).

Other info

To uninstall autoenv, see ./docs/uninstall.md.

To update autoenv, see ./docs/updating.md.

Attributions

Autoenv was originally created by @kennethreitz. Later, ownership was transfered to @inishchith. As of August 22nd, 2021, Edwin Kofler (@hyperupcall) owns and maintains the project.

Repositórios relacionados
direnv/direnv

unclutter your .profile

GoGo ModulesMIT Licensedirenvenvironment
direnv.net
15.3k801
vlucas/phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.

PHPPackagistBSD 3-Clause "New" or "Revised" Licensehacktoberfestphp
13.5k661
kentcdodds/cross-env

🔀 Cross platform setting of environment scripts

TypeScriptnpmMIT Licenseenvironmentvariables
npmjs.com/package/cross-env
6.5k265
caarlos0/env

A simple, zero-dependencies library to parse environment variables into structs

GoGo ModulesMIT Licensegolangenvironment-variables
pkg.go.dev/github.com/caarlos0/env/v11
6.3k280
lando/lando

A development tool for all your projects that is fast, easy, powerful and liberating

MIT Licenselocaldevelopment
lando.dev
4.2k518
symfony/dotenv

Registers environment variables from a .env file

PHPPackagistMIT Licensephpsymfony
symfony.com/dotenv
3.8k28
kizniche/Mycodo

An environmental monitoring and regulation system

PythonPyPIGNU General Public License v3.0raspberry-pisensors
kylegabriel.com/projects/
3.3k564
zjunlp/LLMAgentPapers

Must-read Papers on LLM Agents.

agentsawsome-list
3.1k184
supabase/cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.

TypeScriptnpmclidatabase
supabase.com/docs/reference/cli/about
2.4k500
af/envalid

Environment variable validation for Node, Bun, and other compatible JS runtimes

TypeScriptnpmMIT Licensenodenodejs
1.6k69
pydantic/pydantic-settings

Settings management using pydantic

PythonPyPIMIT Licensepydanticconfiguration
pydantic.dev/docs/validation/latest/concepts/pydantic_settings/
1.4k165
geerlingguy/drupal-vm

A VM for Drupal development

JinjaMIT Licenseansibledrupal
drupalvm.com
1.4k621