| Lesson 2 |
An interactive “script” on the command line |
| Objective |
Execute script commands without creating a script file. |
Interactive Shell “Scripts” at the Command Line
A shell script is normally a collection of commands stored in a file so you can run the same workflow repeatedly.
However, Unix shells also let you execute many “script-like” structures interactively—directly at the prompt—without creating a script file.
This lesson shows how to enter multi-line control structures (such as if statements) at the command line, how to recognize when the shell is waiting for more input, and how to use modern interactive techniques (one-liners, grouping, and here-documents) to complete real tasks quickly and safely.
Control Structures Can Run Interactively
Shell scripting features like conditional tests and loops are not limited to script files. You can enter them directly into a terminal session to:
- test logic before committing it to a script
- perform a one-time task that needs branching or repetition
- experiment with shell syntax and exit statuses
When you start a construct that requires additional input, the shell does not execute anything until the construct is complete. Most shells display a continuation prompt while they wait for the remaining lines.
In many configurations this is shown as
> and is controlled by the
PS2 prompt setting.
Example: start an interactive
if statement (the shell will wait for you to finish it):
$ if [ -e /etc/passwd ]
>
Recognizing the Continuation Prompt
When the prompt changes (often to
>), the shell is telling you:
“I’m still parsing your command—keep entering lines until the structure is complete.”
Standard primary prompts are commonly
$ (regular user) and
# (root). The continuation prompt is typically
>, but it can vary by shell and configuration.
Continue the
if block by entering the remaining keywords and commands:
> then
> cp /etc/passwd /tmp/passwd.backup
> fi
After you enter
fi, the shell has a complete structure and executes the entire block.
Interactive Example: Safe File Check and Copy
The safest interactive examples avoid writing into
/etc or altering system log files.
The following pattern demonstrates the same logic flow (test → then/else → action) using a temporary location:
$ if [ -f /etc/hosts ]; then
> cp /etc/hosts /tmp/hosts.backup
> else
> echo "File not found: /etc/hosts"
> fi
This is still “script logic,” but executed directly in your terminal session.
Entering Script Commands at the Command Line
Try this guided sequence to see interactive parsing in action:
- Start a POSIX shell (optional, for consistency across systems):
sh
- Begin a multi-line conditional:
if [ -f /etc/hosts ]
- Enter:
then
- Enter:
echo "Found /etc/hosts"
- Enter:
else
- Enter:
echo "Missing /etc/hosts"
- End the block:
fi
- Observe that the shell executes only after
fi completes the structure.
Modern “No-File Script” Techniques
Interactive scripting becomes even more powerful when you combine shell features designed for one-off workflows:
- One-liner conditionals: use
&& and || based on exit status
- Grouping commands: run a block without a script file
- Subshells: isolate environment changes (like
cd)
- Here-documents: feed multi-line input to a command
- Inline functions: define a tiny helper for your current session
Exit-status one-liners
$ [ -r /etc/passwd ] && echo "passwd readable" || echo "passwd not readable"
Grouping (runs in current shell)
$ { echo "Start"; date; echo "End"; }
Subshell (runs in isolated environment)
$ ( cd /tmp && pwd && ls )
Here-document (multi-line input without a file)
$ cat << 'EOF'
Line 1
Line 2
Variables like $HOME are not expanded in single-quoted EOF blocks.
EOF
Inline function (session-only)
$ mkcd() { mkdir -p "$1" && cd "$1"; }
$ mkcd /tmp/demo-shell
Historical Note: “Cleanup” Examples from Older Texts
Many classic shell scripting books demonstrate cleanup scripts that truncate files under
/var/log.
On modern Linux systems, logs are commonly managed by
logrotate and/or
systemd-journald, and manually truncating logs can be unsafe (loss of diagnostics, compliance issues, and unexpected service behavior).
The following examples are preserved for historical context, but should not be used on production systems.
Example (legacy): Cleanup commands typed interactively
# Cleanup (legacy example)
# Run as root (legacy note).
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Log files cleaned up."
Example (legacy): Cleanup script file pattern
#!/bin/bash
# Cleanup, version 2 (legacy example)
LOG_DIR=/var/log
cd "$LOG_DIR" || exit 1
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
exit
Modern alternatives include using
logrotate configuration, or for journald-based systems, querying and vacuuming logs with
journalctl according to your organization’s retention policy.
The next lesson defines what changes a text file into a shell script (including headers, permissions, and execution behavior).
