DEV Community

Cover image for Just enough bash to be dangerous

Just enough bash to be dangerous

Hugo Di Francesco on July 26, 2018

A bash cheat sheet for developers who just want to get by. Writing setup, CI and deployment flows means a bit of the old bash scripting. Despit...
Collapse
 
moopet profile image
Ben Sinclair

You should put spaces between the test brackets and the test conditions like [ -f foo.txt ] otherwise you'll get a command not found error.

The environment variable one is over-complicated - you don't need the else, and you don't need to use false to ensure that the last command is run if you use a semicolon:

# long
if [[ -z "${CIRCLE_BRANCH}"] ]; then
    npm run redis-cli flushall
fi

npm run sync


# one-liner
[-z "${CIRCLE_BRANCH}"] && npm run redis-cli flushall; npm run sync
Collapse
 
hugo__df profile image
Hugo Di Francesco • Edited

Thanks I've updated it.

Collapse
 
nebojsac profile image
Nick Cinger

Good stuff! I feel like I know how to get around Bash, but I keep learning new tricks ever day, and I learned a few here, thanks!

Also, seeing that you have to end case block with the reverse esac just makes me giggle!

Collapse
 
vlasales profile image
Vlastimil Pospichal

I don't like ask the user. The best answer is the one I will not ask.

Collapse
 
blvkoblsk profile image
BLVKOBLSK

BASH Docs are your best friend; especially peeping others legit scripts too of course.

Thanks for this insight too!