Stage 7 · Lesson 3 of 8
Variables, arguments and input
In a script, set a with name="value", with no spaces around the =. Read it back with "$name", always inside . are the values passed to the script when you run it.
$1 is the first, $2 the second, "$@" is all of them and $# is how many there are. ${name:-default} gives a fallback value when a variable is empty. $(command) captures a command's output into a variable.
Check the arguments at the top of the script and print a short usage line if any are missing. Read secrets from , not from arguments, because arguments show up in lists and shell history.
Common trapunquoted variables, which break on spaces and turn empty values into dangerous commands.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- GuideFreeGreg's Wiki: BashGuide
Greg's Wiki (Wooledge) · Beginner
A careful, accurate Bash guide that teaches the habits that avoid most scripting bugs
- CourseFreeThe Missing Semester of Your CS Education
MIT · Beginner
The shell tools and scripting lecture, with exercises that build useful small scripts
Practise
Keep handy
Also worth your time
- roadmap.sh: Shell / BashGo deeper
The scripting side of the shell map: arrays, traps, parameter expansion and more