Shell Scripting Basics Print

  • 0

Automating with scripts.

WHAT A SCRIPT IS

A file of commands, run in sequence.

WHAT THE FIRST LINE SHOULD BE

A shebang naming the interpreter.

WHY

It ensures the script runs under the shell you wrote it for.

WHAT TO SET AT THE TOP OF ANY SERIOUS SCRIPT

Exit on error Exit on undefined variables Failure of any command in a pipeline

WHY THOSE THREE

Without them, a script continues after failures and produces wrong results silently.

WHAT VARIABLES LOOK LIKE

A name, an equals sign, no spaces around it.

HOW TO USE ONE

A dollar sign, and braces around the name.

WHY BRACES

They remove ambiguity when adjacent to other text.

WHAT TO ALWAYS DO WITH VARIABLES IN COMMANDS

Quote them.

WHY

Unquoted variables break on spaces and expand wildcards unexpectedly.

WHAT CONDITIONALS LOOK LIKE

if, a test, then, and fi.

WHAT TESTS ARE COMMON

Whether a file exists Whether a string is empty Numeric comparison

WHAT LOOPS LOOK LIKE

for over a list, or while on a condition.

WHAT EXIT CODES MEAN

Zero is success; anything else is failure.

WHAT TO RETURN

A non-zero code when your script fails.

WHY

Whatever runs it needs to know.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot