Skip to content

Instantly share code, notes, and snippets.

@jordanwade90
Created January 24, 2015 06:50
Show Gist options
  • Save jordanwade90/0ad928a60ae1928851f5 to your computer and use it in GitHub Desktop.
Save jordanwade90/0ad928a60ae1928851f5 to your computer and use it in GitHub Desktop.
Function for the Plan 9 shell rc(1) to invoke a command with sh(1) and print out the specified environment variables in a form rc can read. I use rc for my shell but need to set up my environment like I was running sh.
# eval-for-environ COMMAND [VARIABLE...]
# Evaluates COMMAND using sh then arranges for rc to learn the values
# of each of the environment variables VARIABLE.... This will break if
# the value of any VARIABLE may contain a quote (').
fn eval-for-environ{
cmd=$1
shift
for(var){
cmd=($cmd ';' echo $var'=\''"$(echo $'$var' | sed s/\''/\''\''/g)"\''')
}
echo $"cmd | sh
}
# Example:
# if(~ $#DBUS_SESSION_BUS_ADDRESS 0) {
# eval `{eval-for-environ 'eval $(dbus-launch --exit-with-session)' DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID}
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment