Skip to the content ForoHackers
Log inJoin

Shell scripting: start here

Shell scripting
#583 · Ophuscado · · Report

A shell is a command interpreter, and shell scripting is writing sequences of commands in a file to automate work. It glues together small programs through files and pipes, which makes it the fastest tool for system chores, build steps and deployments. It is also easy to get subtly wrong, so habits of care matter more than cleverness.

Key concepts

  • Commands and pipes: small programs read input and write output, and a pipe feeds one into the next.
  • Streams and redirection: standard input, output and error, redirected to files or other commands.
  • Variables and quoting: values are text; unquoted expansion splits on spaces, the source of many bugs.
  • Exit status: every command returns a number, zero for success, and scripts branch on it.
  • Control flow: conditionals, loops, functions and tests of files and strings.
  • Portability: the POSIX shell versus extensions of specific shells.

Vocabulary

  • Shebang: the first line of a script naming the interpreter that runs it.
  • Glob: a pattern such as *.txt that the shell expands to file names.
  • Subshell: a child shell that runs a group of commands separately.
  • Environment variable: a named value inherited by child processes.

What to study, in order

  1. Daily use of the terminal: files, permissions, searching, and the core text tools.
  2. Pipes and redirection; solve small text problems with a chain of commands.
  3. Write a script with arguments, variables, conditionals and loops, quoting everything.
  4. Error handling: exit codes, strict modes and cleanup of temporary files.
  5. Lint scripts with a static checker and read what each warning means.
  6. Know when to stop: move to a general-purpose language when a script grows complex.

Related tags

#linux #sysadmin #python #devops

Log in or join to reply.