Stack Juice2
Stack Juice2

Reputation: 77

Create a bash script to compile SCSS into CSS that takes arguments

How can I create a bash script to compile SCSS into CSS that works with different filename and paths?

For example I'm running this command to compile my SCSS file into a CSS file in a different directory:

sass test.scss ../css/test2.css

It's tedious to run it over and over again, so what can I do to write something that makes this easier? I'm new to bash scripting.

Upvotes: 1

Views: 1759

Answers (1)

sebnukem
sebnukem

Reputation: 8333

Is that what you are looking for?

#!/bin/bash
f=${1:-test}
sass ${f}.scss ../css/${f}.css

This script runs sass FILENAME.scss ../css/FILENAME.css. If FILENAME (without extension) isn't set by the first argument it defaults to test. You can easily update it to accept more than one input file.

But you are not going to save much time since you still have to call that script instead of sass. Replacing a command by another doesn't accomplish anything.

Use !sass instead to rerun the last sass command on the same file or !! to rerun the previous command. Look into bash history.

How to use:
Save this script into a file, say xx.
Make xx executable: chmod u+x xx
Run it without argument on test: ./xx
Run it with a filename without extension: ./xx myscssfile

Here's another version that will take a list of filenames as input or default to test.scss:

#!/bin/bash
function dosass {
    if [ $# -eq 0 ]; then return; fi
    b=${1%.scss}
    sass $b.scss ../css/$b.css
}
if [ $# -eq 0 ]; then
    dosass "test.scss"
else
    for f; do dosass $f; done
fi

Upvotes: 1

Related Questions