Skip to content

Instantly share code, notes, and snippets.

@hangj
Forked from overtrue/init.fish
Last active December 8, 2023 11:34
Show Gist options
  • Save hangj/877bd596715c2cd6139c80e10b9f7dca to your computer and use it in GitHub Desktop.
Save hangj/877bd596715c2cd6139c80e10b9f7dca to your computer and use it in GitHub Desktop.
config.fish set environment variables from bash_profile
# Fish shell
# REUSE ENVIRONMENT VARIABLES FROM ~/.bash_profile
bash -c '. ~/.bash_profile; env' | while read e
set var (echo $e | sed -E "s/([a-zA-Z0-9_]+)=(.*)\$/\1/")
set value (echo $e | sed -E "s/([a-zA-Z0-9_]+)=(.*)\$/\2/")
# remove surrounding quotes if existing
set value (echo $value | sed -E "s/^\"(.*)\"\$/\1/")
if test $var = "PATH"
# replace ":" by spaces. this is how PATH looks for Fish
set value (echo $value | sed -E "s/:/ /g")
# escape '(' and ')'
set value (echo $value | sed -E "s/\(/\\\(/g" | sed -E "s/\)/\\\)/g")
# use eval because we need to expand the value
eval set -xg $var $value
continue
end
# evaluate variables. we can use eval because we most likely just used "$var"
set value (eval echo $value)
#echo "set -xg '$var' '$value' (via '$e')"
switch $value
case '`*`';
# executable
set NO_QUOTES (echo $value | sed -E "s/^\`(.*)\`\$/\1/")
set -x $var (eval $NO_QUOTES)
case '*'
# default
switch $var
case 'PWD'
case 'SHLVL'
case '_'
case '*'
# escape '(' and ')'
set value (echo $value | sed -E "s/\(/\\\(/g" | sed -E "s/\)/\\\)/g")
eval set -xg $var $value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment