badrelmers / RefrEnv

Refresh the environment. Reload environment variables every time you want environment changes to propagate, so you do not need to restart the console after setting a new variable. A better alternative to the chocolatey refreshenv. for cmd, bash, powershell and zsh.
GNU General Public License v3.0
53 stars 10 forks source link
bash cmd cygwin environment-variables powershell zsh

RefrEnv - Refresh the Environment

Reload environment variables inside CMD, Bash, Powershell or Zsh every time you want environment changes to propagate, so you do not need to restart them after setting a new variable with setx or after installing a new app which adds new variables.

This is a better alternative to the Chocolatey refreshenv for cmd (and works for bash and zsh too (cygwin, Msys2 and GitBash)), which solves several problems in the chocolatey's refreshenv, like:

and more...

Usage:

git clone https://github.com/badrelmers/RefrEnv
cd RefrEnv

cmd

Works in all windows versions xp+

call refrenv.bat

Powershell

Works in all Powershell versions V2+

. .\refrenv.ps1

bash:

source refrenv.sh

Zsh:

source refrenvz.sh

Details:

Opciones for RefrEnv in CMD

NAME
   RefrEnv - Refresh the Environment for CMD

SYNOPSIS
   call refrenv.bat

DESCRIPTION
   By default with no arguments, this script will do a full 
   refresh (refresh all non critical variables*, and refresh the PATH).

   you can use the following variables to change the default behaviour:

   RefrEnv_ResetPath=yes       Reset the actual PATH inside CMD, then refresh
                               it with a new PATH. This will delete any PATH 
                               added by the script who called RefrEnv. It is 
                               equivalent to running a new CMD session.

   RefrEnv_debug=yes           Debug what this script do. The folder containing
                               the files used to set the variables will be
                               open, then see _NewEnv.sh, this is the file
                               which run inside your script to setup the new
                               variables, you can also revise the intermediate
                               .txt files.

Opciones for RefrEnv in Powershell

NAME
    RefrEnv - Refresh the Environment for Powershell/Pwsh

SYNOPSIS
    . .\refrenv.ps1

DESCRIPTION
    By default with no arguments, this script will do a full 
    refresh (refresh all non critical variables*, and refresh the PATH).

    you can use the following variables to change the default behaviour:

    RefrEnv_ResetPath=yes       Reset the actual PATH inside Powershell, then refresh
                                it with a new PATH. This will delete any PATH 
                                added by the script who called RefrEnv. It is 
                                equivalent to running a new Powershell session.

Opciones for RefrEnv in Bash and Zsh

SYNOPSIS
    source refrenv.sh
    source refrenvz.sh

DESCRIPTION
    By default with no arguments, RefrEnv will do a full 
    refresh (refresh all non critical variables*, and refresh the PATH).

    you can use the following variables to change the default behaviour:

    RefrEnv_StrictRefresh=yes   Strict mode (secure refresh). this prevent refreshing a
                                variable if it is already defined in the actual bash/zsh session. 
                                The PATH will be refreshed.

    RefrEnv_ResetPath=yes       Reset the actual PATH inside bash/zsh, then refresh it with a new PATH.
                                this will delete any PATH added by the script who called RefrEnv. 
                                it is equivalent to running a new bash/zsh session.

    RefrEnv_debug=yes           Debug what RefrEnv do. The folder containing the 
                                files used to set the variables will be open, then see 
                                _NewEnv.sh this is the file which run inside your script
                                to setup the new variables, you can also revise the 
                                intermediate .txt files.

    RefrEnv_help=yes            Print the help.

    RefrEnv support the so called bash Strict Mode like: "set -eEu -o pipefail ; shopt -s inherit_errexit"
    you can use the Strict Mode safely in your parent script without worry.

[!NOTE] *critical variables: are the built-in variables which belong to cmd/bash/zsh or windows and should not be refreshed normally like:

Expand for details - windows vars: ALLUSERSPROFILE APPDATA CommonProgramFiles CommonProgramFiles(x86) CommonProgramW6432 COMPUTERNAME ComSpec HOMEDRIVE HOMEPATH LOCALAPPDATA LOGONSERVER NUMBER_OF_PROCESSORS OS PATHEXT PROCESSOR_ARCHITECTURE PROCESSOR_ARCHITEW6432 PROCESSOR_IDENTIFIER PROCESSOR_LEVEL PROCESSOR_REVISION ProgramData ProgramFiles ProgramFiles(x86) ProgramW6432 PUBLIC SystemDrive SystemRoot TEMP TMP USERDOMAIN USERDOMAIN_ROAMINGPROFILE USERNAME USERPROFILE windir SESSIONNAME - bash vars: BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND BASH_COMPLETION_VERSINFO BASH_LINENO BASH_REMATCH BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION COLUMNS COMP_WORDBREAKS CYGWIN CYG_SYS_BASHRC DIRSTACK EUID EXECIGNORE FUNCNAME GROUPS HISTCMD HISTCONTROL HISTFILE HISTFILESIZE HISTSIZE HISTTIMEFORMAT HOME HOSTNAME HOSTTYPE IFS INFOPATH LANG LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME LINENO LINES MACHTYPE MAILCHECK OLDPWD OPTERR OPTIND ORIGINAL_PATH OSTYPE PATH PIPESTATUS POSIXLY_CORRECT PPID PRINTER PROFILEREAD PROMPT_COMMAND PS0 PS1 PS2 PS3 PS4 PWD RANDOM SECONDS SHELL SHELLOPTS SHLVL SSH_ASKPASS TERM TERM_PROGRAM TERM_PROGRAM_VERSION TZ UID USER _backup_glob CHILD_MAX BASH_COMPAT FUNCNEST COMP_TYPE COMP_KEY READLINE_LINE_BUFFER READLINE_POINT PROMPT_DIRTRIM BASH_EXECUTION_STRING COPROC_PID COPROC GLOBIGNORE HISTIGNORE SRANDOM READLINE_MARK EPOCHSECONDS EPOCHREALTIME BASH_ARGV0 COMPREPLY COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS EMACS FCEDIT FIGNORE HOSTFILE IGNOREEOF INPUTRC INSIDE_EMACS MAPFILE READLINE_LINE REPLY TIMEFORMAT TMOUT TMPDIR histchars



RefrEnv was created to respond to this Stackoverflow thread: https://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w