1

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.

5
  • Use bash history? Sorry I don't understand the question... Commented Mar 16, 2015 at 21:23
  • @sebnukem I want to create a shortcut basically to run this command over and over again. BUT I want the shortcut to also work with different filenames not just test.scss? make sense? Commented Mar 16, 2015 at 21:25
  • 1
    Did you try anything at all before asking this question? "I'm new" is not a valid excuse for not trying anything. Commented Mar 16, 2015 at 21:31
  • @cimmanon Yes I have tried a few different ways that I didn't list Commented Mar 16, 2015 at 21:36
  • @StackJuice2 So you should show that, because right now it just looks like an incredibly lazy question. "I tried this code, but I got this error" or "I tried this code, but it gave me this result (which is totally wrong)". Commented Mar 16, 2015 at 21:53

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

So is it worth trying to write a script in your opinion? Just trying to be efficient.
Ok I understand. Even if I did use the command you suggested, how would I call it??
thanks for the help, I'm new and frankly the tutorials on this subject aren't good from what I've seen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.