Skip to the content ForoHackers
Log inJoin

Shell scripting: start here

Shell scripting
#3089 · · Quote · 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

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply