Skip to content

Instantly share code, notes, and snippets.

@sstephenson
Created September 27, 2011 17:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sstephenson/1245638 to your computer and use it in GitHub Desktop.
Save sstephenson/1245638 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Reads lines from stdin in the format `VAR=value` and escapes
# them for the shell, prepending each line with `export`.
# Parameter substitution is allowed in `value` with `$VAR` and
# `${VAR}` syntax. You can escape `$` and `\` with a backslash.
sed \
-e "/^[ "$'\t'"]*[A-Za-z_][0-9A-Za-z_]*=/ !d" \
-e "s/'/'\\\\''/g" \
-e "s/\(\\\\\\\$\)/'\\1'/g" \
-e "s/\\\\\\\\/\\\\/g" \
-e "s/\(\\\$[0-9A-Za-z_][0-9A-Za-z_]*\)/'\\1'/g" \
-e "s/\(\\\${[0-9A-Za-z_][0-9A-Za-z_]*}\)/'\\1'/g" \
-e "s/^[ "$'\t'"]*\([A-Za-z_][0-9A-Za-z_]*\)=\(.*\)$/export \\1='\\2'/"
@airblade
Copy link

Obviously.

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