Shell script to parse variable definitions from Scala build file The build.sbt text file contains versions like this:

name := "happy"
scalaVersion := "2.11.8"
sparkVersion := "2.2.0"


I wrote a Bash script to parse out the PROJECT_NAME and SCALA_VERSION from the build.sbt file:

PROJECT_NAME=$(cat build.sbt | grep "name :=" | cut -f 3 -d " " | tr -d '"')
SCALA_VERSION=$(cat build.sbt | grep "scalaVersion :=" | cut -f 3 -d " " | tr -d '"')


How can I write this more elegantly / robustly?  I'm ok with an awk or sed approach, but don't want to add an external dependency to the script.

Here's some more of the script to see how the variables are being used.

if [ "$SCALA_VERSION" = "" ]
  then
    echo "SCALA_VERSION variable cannot be empty"
    exit 1
fi

SCALA_BINARY_VERSION=${SCALA_VERSION%.*}
if [ "$SCALA_BINARY_VERSION" = "" ]
  then
    echo "SCALA_BINARY_VERSION variable cannot be empty"
    exit 1
fi

echo "Create a GitHub release"
JAR_PATH=target/scala-${SCALA_BINARY_VERSION}/${PROJECT_NAME}_${SCALA_BINARY_VERSION}-${SPARK_VERSION}_${PROJECT_VERSION}.jar
hub release create -a $JAR_PATH -m "Release v${PROJECT_VERSION}" v${PROJECT_VERSION}
I'd use awk to generate shell syntax, and source it in the shell:

$ source &lt;(
    awk '
        $1 == "name"         {print "PROJECT_NAME=" $NF}
        $1 == "scalaVersion" {print "SCALA_VERSION=" $NF}
    ' build.sbt
)

$ echo $PROJECT_NAME,$SCALA_VERSION
happy,2.11.8


If the name or version strings might contain spaces, then we need to be more specific about awk's field separator:

$ cat build.sbt
name := "hello world"
scalaVersion := "2.11.8"
sparkVersion := "2.2.0"

$ awk '
    $1 == "name"         {print "PROJECT_NAME=" $NF}
    $1 == "scalaVersion" {print "SCALA_VERSION=" $NF}
' build.sbt

PROJECT_NAME=world"              # &lt;&lt; oops
SCALA_VERSION="2.11.8"

$ awk -F '"' '
    $1 ~ /^name/         {printf "PROJECT_NAME=\"%s\"\n",  $2}
    $1 ~ /^scalaVersion/ {printf "SCALA_VERSION=\"%s\"\n", $2}
' build.sbt

PROJECT_NAME="hello world"
SCALA_VERSION="2.11.8"