Created
July 16, 2025 15:35
-
-
Save Galeas/502d805b899a01b1f3432625a0b9ba39 to your computer and use it in GitHub Desktop.
Add ENV variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ----------------------------------------- | |
| # Create permanent environment variable | |
| # | |
| # $1 - variable name | |
| # $2 - variable definition | |
| # @requires: '~/.zshrc' or '~/.bashrc' | |
| # ----------------------------------------- | |
| function new_env_var { | |
| local detected_shell="$(ps -o comm= -p $$)" | |
| local rcfile=$(echo "${HOME}/.${detected_shell//-/}rc") | |
| if [[ -f $rcfile ]]; then | |
| local var_name; local var_val | |
| if [ -z "$1" ]; then | |
| echo "variable name (e.g. 'NEW_VAR' for '\$NEW_VAR'):"; read var_name | |
| else | |
| var_name=$1 | |
| fi | |
| if [ -z "$2" ]; then | |
| echo "variable definition:"; read var_val | |
| else | |
| local s2 var_val="${@//${var_name}/}" | |
| until s2="${var_val#[ ]}"; [ "$s2" = "$var_val" ]; do var_val="$s2"; done | |
| fi | |
| echo "export $var_name=\"$var_val\"" >> $rcfile; export $var_name="$var_val" | |
| else | |
| echo "No $detected_shell runcom file found. Please make sure $rcfile exists." | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment