Skip to content

Instantly share code, notes, and snippets.

@shivanshs9
Created February 13, 2025 06:49
Show Gist options
  • Save shivanshs9/d16906673d733671277961334c906a6f to your computer and use it in GitHub Desktop.
Save shivanshs9/d16906673d733671277961334c906a6f to your computer and use it in GitHub Desktop.
Fish function to export env vars from bash-like command output. Basically, equivalent to source command but with support of var=value.
function set-env --description "Runs the command and sets the environment variables from the output. Kinda like source, but with additional support of var=value"
if not set -q argv[1]
echo "set-env: no command specified"
return 1
end
set cmd "$argv[1]"
eval "$cmd" | while read -l line
if test -z "$line"
continue
end
set -l v (string split -m 1 "=" -- $line)
switch (count $v)
case 1
set -gx $v $$v
case 2
if contains -- $v[1] PATH CDPATH MANPATH
set -l colonized_path (string replace -- "$$v[1]" (string join ":" -- $$v[1]) $v[2])
set -gx $v[1] (string split ":" -- $colonized_path)
else
# status is 1 from the contains check, and `set` does not change the status on success: reset it.
true
set -gx $v[1] $v[2]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment