Physics · Introductory

Shell Scripting

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

Shell scripting automates repetitive system commands, allowing physicists to offload administrative tasks and maintain focus on high-performance code. Scripts are executed via interpreters specified by a shebang line and require explicit execute permissions. The language supports variables, arrays, and external tool integration to handle data processing and user interaction.

Intuition

Think of shell scripting as a macro recorder for your terminal that can make decisions and loop. Instead of typing the same compilation or analysis commands repeatedly, you write them once in a file. The operating system reads the first line (shebang) to know which interpreter to use, checks if you have permission to run it, and then executes the commands in order. You can feed data into these commands using files or inline text, store results in variables, and even call powerful external tools like awk for math, all without leaving the shell environment.

Derivation

  1. Define the purpose: Automate repetitive sequences to keep high-performance code clean.
  2. Establish execution protocol: Use a shebang (e.g., #!/bin/tcsh) to specify the interpreter.
  3. Set permissions: Apply chmod u+x to allow execution.
  4. Handle input: Use redirection (<) or Here Documents (<<EOF) to provide data.
  5. Store data: Use set name = value for variables and set name = (val1 val2) for arrays.
  6. Process data: Use foreach loops to iterate over arrays.
  7. Make decisions: Use if ( condition ) with tests like -f for file existence.
  8. Integrate tools: Pass shell variables to external tools like awk using -v.

Worked example

Problem

Create a script to compile and run four C++ programs (area_01.cpp to area_04.cpp) using an array of filenames and a Here Document for input.

  1. Start with shebang #!/bin/tcsh -f.
  2. Define an array files containing the four C++ source filenames.
  3. Use a foreach loop to iterate through files.
  4. Inside the loop, compile each file using g++ $file -o area.
  5. Run the executable ./area and pipe input using <<EOF ... EOF.
  6. Include numerical values from an array R in the Here Document.

Answer The script automates the compilation and execution of four programs, passing input data from an array via a Here Document without manual intervention or temporary files.

Common mistakes

  • Indenting the closing Here Document marker (e.g., EOF); it must be at the start of the line.
  • Forgetting to run chmod u+x before executing the script, causing 'Permission denied'.
  • Using single quotes around variables intended for expansion; use double quotes instead.
  • Incorrect indentation in loops and conditionals leading to syntax errors.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.