Skip to content

Instantly share code, notes, and snippets.

@Debilski
Created July 12, 2014 13:07
Show Gist options
  • Save Debilski/c0ed3f76839daa67674e to your computer and use it in GitHub Desktop.
Save Debilski/c0ed3f76839daa67674e to your computer and use it in GitHub Desktop.
function bash_env --description 'Imports named variables from a bash script'
# Usage:
# bash_env script.bash VAR1 VAR2 VARN
#
set -l script $argv[1]
while read i; set code "$code""$i"\n; end < $script
set -l variables
for arg in $argv[2..-1]
set variables $variables "echo \"set -g $arg \${$arg}\""\n
end
set code "$code""$variables"
set res (bash -c $code)
echo "Importing" (count $res) "variable(s)"
for val in $res
echo "$val"
eval "$val"
end
end
@Debilski
Copy link
Author

$ echo "export VAR_ONE=1; export VAR_TWO=2" > vars.bash
$ echo $VAR_ONE ; echo $VAR_TWO


$ bash_env vars.bash VAR_ONE VAR_TWO
Importing 2 variable(s)
set -g VAR_ONE 1
set -g VAR_TWO 2
$ echo $VAR_ONE ; echo $VAR_TWO
1
2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment