Shell script

Shell script

A shell script is a text file containing a list of computer commands that the machine executes one after another. It is used to automate tasks that would otherwise have to be typed by hand each time.

Many tasks on a computer can be typed instead of clicked. For this there is a program that accepts commands in text form and executes them: the shell, often also called the command line or terminal. For example, you write a command to copy a file, press Enter, and the file is copied. A shell script is nothing more than a text file in which several such commands are listed one below the other. When you start this file, the computer works through the commands from top to bottom. Instead of typing twenty lines individually, you call the script once.

Why experts pour their work into text files

The most important reason is repeatability. A person who types in twenty commands by hand will eventually make a typo or forget a step. A script does exactly the same thing every time, today and in two years. This is exactly what data centers, servers, and development teams rely on.

The second reason is traceability. A script is readable text that can be placed into a version control system. This is an archive that records every change with date and author. If something breaks, you can see who changed what and when. With manual work in the terminal, this trail does not exist.

A shell script is not a fully-fledged program in the classic sense. It rather controls other programs, much like a director who gives instructions but does not act himself. For computational work, languages like Python are better suited. For stringing together existing tools, the shell is unbeatably concise.

From command to executable file

A script usually begins with a special first line, the so-called shebang. It looks like this: #!/bin/bash. This states within the file itself which program should interpret the rest. Bash is the most common shell on Linux systems; on Macs, Zsh is typical, which works very similarly.

Below that follow the commands. In addition to simple calls, there are variables that store values temporarily, and loops that apply a command to many files. With a condition, you check whether a folder exists and react accordingly. This gives you everything a programming language needs, just in a very sparse notation.

A typical special feature is the pipe, written as a vertical bar. It feeds the result of one command directly as input into the next. This creates chains of small tools that together accomplish something complex. The most common beginner mistake, by the way, is handling spaces in filenames: without quotation marks, the shell splits a name into two parts.

Shell scripts in servers, cloud, and AI projects

Anyone who operates a website or an online service can hardly avoid shell scripts. They create backups at night, delete old log files, and restart services. Started on a schedule, they run without any human intervention. A large part of server maintenance consists of such small, unspectacular files.

They are also everyday practice in AI projects. A training run, in which a model learns from example data, is almost always started via a script. It loads the data, sets the configurations, and writes away the results. In tutorials on platforms like GitHub, there is therefore often a line that you are supposed to copy and paste into the terminal.

A word of caution: running a script from the internet without checking it is risky. It runs with the user’s permissions and can delete files or download software. This is exactly why shell scripts also appear in security alerts, when attackers use them to inject malware. Taking a look inside the file before starting it is not excessive caution, but standard practice.

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.