# direnv



https://direnv.net



### Installation:

```bash
$ brew install direnv
```

Ajouter à la fin du `.bashrc`

```bash
eval "$(direnv hook bash)"
```

ou du `.zshrc`

```bash
eval "$(direnv hook zsh)"
```



### Utilisation:

Création de la variable d'environnement dans le fichier `.envrc`:

```bash
~/Documents/Scripts/bash master*
❯ echo export PASSWORD="34diK=[6]Zui" > .envrc
direnv: error /Users/bruno/Documents/Scripts/bash/.envrc is blocked. Run `direnv allow` to approve its content
```

Il faut autoriser l'utilisation du fichier `.envrc`:

```bash
~/Documents/Scripts/bash master*
❯ direnv allow .
direnv: loading ~/Documents/Scripts/bash/.envrc
direnv: export +PASSWORD
```

La variable PASSWORD est disponible dans le répertoire bash:

```bash
~/Documents/Scripts/bash master* 13s
❯ echo ${PASSWORD}
34diK=[6]Zui
```

mais plus en dehors:

```bash
~/Documents/Scripts/bash master*
❯ cd ..
direnv: unloading

~/Documents/Scripts master*
❯ echo ${PASSWORD}

```

Création du script:

```bash
#!/bin/bash
...
BKP_PASS="${PASSWORD}"
...
```

Lancer le script:

```bash
~/Documents/Scripts master* 2m 0s
❯ cd bash && ./backup_mysql.sh
direnv: loading ~/Documents/Scripts/bash/.envrc
direnv: export +PASSWORD

# la variable PASSWORD est dispo.
```

