Structure for Shell and Bashs
Script structure is how a Bash program is organised for reuse: functions, positional arguments, option parsing, and reading input from users, pipes and files. It sits between simple scripts and robust tooling, since well-structured scripts take parameters, return exit codes and read data line by line instead of hardcoding paths.
Stage: Bash Scripting · Part of the Shell and Bash roadmap · Last reviewed 19 November 2026
What to learn in Structure
- Define functions, pass arguments with $1 and $@, and return status codes
- Parse command-line options with getopts and print a usage message
- Read a file line by line with while read without mangling whitespace
- Accept user input using read and handle input piped from other commands
- Use local variables inside functions to avoid leaking state
Structure interview questions
What is the difference between $@ and $*?
Both expand to all positional parameters. When quoted, "$@" keeps each argument as a separate word, preserving spaces inside them, while "$*" joins them into a single string. Use "$@" when forwarding arguments to another command.
How do you read a file line by line safely in Bash?
Use while IFS= read -r line; do ...; done < file. Setting IFS to empty keeps leading and trailing whitespace, and -r stops backslashes from being treated as escapes. Avoid for line in $(cat file), which splits on every space.
Where Structure fits in the Shell and Bash roadmap
Structure belongs to the Bash Scripting stage (beginner level), which takes about 12 hours. Automate.
Other units in this stage
Back to the full Shell and Bash roadmap