tzenderman
tzenderman

Reputation: 2663

Bash script: Using sed to stream a Environment variable to a file

I am using the sed command in a bash script to stream a COMMIT_SHA value into a python file. Before running this bash script, the settings.py file has this line:

COMMIT_SHA = ""

After the bash script it needs to have the actual commit sha like so:

COMMIT_SHA = "56e05fced214c44a37759efa2dfc25a65d8ae98d"

Doing this in python would be easy, but I need to use Bash to get this done. The following is the approach I've tried:

COMMIT_SHA=`git rev-parse HEAD`
sed $PROJECT_DIR/settings.py "^COMMIT_SHA = .*$" 'COMMIT_SHA = "$COMMIT_SHA"'

However this simply puts COMMIT_SHA = "$COMMIT_SHA" into my settings.py which isn't what I want.

Upvotes: 1

Views: 268

Answers (1)

Oliver Matthews
Oliver Matthews

Reputation: 7823

It's because you have enclosed the final line in ''s.

Instead, try

sed $PROJECT_DIR/settings.py "^COMMIT_SHA = .*$" "COMMIT_SHA = \"$COMMIT_SHA\""

or, in the more normal form

sed -i "s/^COMMIT_SHA = .*$/COMMIT_SHA = \"$COMMIT_SHA\"/" $PROJECT_DIR/settings.py

Upvotes: 2

Related Questions