sleep / seq
| Since: | sleep | All Linux |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) | ||
| seq | All Linux | |
| macOS(2005 Tiger) | ||
| Bash 1.0(1989) |
sleep pauses script execution for a specified amount of time. seq generates sequences of numbers, making it useful for loops and creating numbered lists. Both are commonly used utilities in shell scripting.
Syntax
sleep takes a number with an optional unit. When no unit is given, the value is treated as seconds.
sleep seconds sleep number[s|m|h|d]
seq supports three forms: a single maximum value, a start and maximum value, or a start, increment, and maximum value.
seq max seq start max seq start increment max
sleep Options
| Usage | Description |
|---|---|
| sleep 5 | Wait 5 seconds. No unit defaults to seconds. |
| sleep 0.5 | Wait 0.5 seconds (500 ms). macOS's built-in sleep does not support decimals (see below). |
| sleep 1m | Wait 1 minute (60 seconds). |
| sleep 1h | Wait 1 hour. |
| sleep 1d | Wait 1 day (24 hours). |
seq Options
| Option | Description |
|---|---|
| -w | Pad numbers with leading zeros to equalize width (01, 02, ... 10). |
| -s string | Use the specified string as the separator. Defaults to newline. |
| -f format | Use a printf-style format string (e.g., %05g). |
Sample Code
Basic usage of sleep. A patrol script for Kamurocho that adds a delay between processing steps.
echo "Checking Kamurocho north area..." sleep 3 echo "Checking Kamurocho south area..." Checking Kamurocho north area... Checking Kamurocho south area...
Waiting in minutes and hours.
sleep 1m && echo "Rechecking in 1 minute" sleep 2h && echo "Rechecking in 2 hours"
Basic usage of seq. With a single argument, it generates numbers from 1 to the given value.
seq 5 1 2 3 4 5
Specifying a start and end value.
seq 3 7 3 4 5 6 7
Specifying an increment (start, increment, end).
seq 1 2 10 1 3 5 7 9
The -w option pads numbers with leading zeros. Useful for generating consistently formatted file or directory names.
seq -w 1 10 01 02 03 04 05 06 07 08 09 10
The -s option sets the separator. This example outputs all numbers on a single line separated by spaces.
seq -s " " 1 5 1 2 3 4 5
A for loop using seq. Processing five clan member log files in sequence.
clan_members.sh
#!/bin/bash
for i in $(seq 1 5); do
echo "Processing clan_member_${i}.txt"
done
Run the following command:
bash clan_members.sh Processing clan_member_1.txt Processing clan_member_2.txt Processing clan_member_3.txt Processing clan_member_4.txt Processing clan_member_5.txt
A retry loop that repeatedly attempts an operation — such as an API call — with a delay between attempts. It tries up to five times and exits the loop on success.
kamurocho_patrol.sh
#!/bin/bash
MAX_RETRY=5
INTERVAL=10
SUCCESS=0
for i in $(seq 1 $MAX_RETRY); do
echo "[$i/$MAX_RETRY] Connecting to server..."
if curl -sf http://192.168.1.1/status > /dev/null; then
echo "Connection succeeded"
SUCCESS=1
break
fi
echo "Connection failed. Retrying in ${INTERVAL} seconds"
sleep $INTERVAL
done
if [ "$SUCCESS" -eq 0 ]; then
echo "Maximum retry count reached" >&2
exit 1
fi
Creating a series of log files with zero-padded names. This example generates battle logs numbered by session.
battle_log.sh
#!/bin/bash
for i in $(seq -w 1 10); do
touch "battle_log_${i}.txt"
echo "Created battle_log_${i}.txt"
done
Run the following command:
bash battle_log.sh Created battle_log_01.txt Created battle_log_02.txt Created battle_log_03.txt Created battle_log_04.txt Created battle_log_05.txt Created battle_log_06.txt Created battle_log_07.txt Created battle_log_08.txt Created battle_log_09.txt Created battle_log_10.txt
Comparison with Brace Expansion
Bash has a built-in brace expansion syntax, {1..10}, that generates sequences in the same way as seq. Because it does not invoke an external command, it is faster and often preferred for simple numeric ranges.
echo {1..5}
1 2 3 4 5
Run the following command:
for i in {1..5}; do echo "member_${i}"; done
member_1
member_2
member_3
member_4
member_5
However, brace expansion has a key limitation: variables cannot be used inside it.
MAX=5
for i in {1..$MAX}; do echo $i; done
{1..5}
When the endpoint needs to come from a variable, use seq instead.
MAX=5 for i in $(seq 1 $MAX); do echo $i; done 1 2 3 4 5
| seq | {1..N} brace expansion | |
|---|---|---|
| Variable support | Yes | No (literals only) |
| Custom increment | Yes (seq 1 2 10) | Yes ({1..10..2}) |
| Zero padding | Yes (-w option) | Yes ({01..10}) |
| External command | Required | Not required (Bash built-in) |
| Decimal sequences | Yes (seq 0.1 0.1 1.0) | No |
Overview
sleep is not a Bash built-in — it is an external command (typically /bin/sleep). A plain number is treated as seconds. GNU coreutils' sleep supports the units s (seconds), m (minutes), h (hours), and d (days), and you can combine multiple values that are summed together (e.g., sleep 1m 30s).
seq is also an external command (typically /usr/bin/seq). With one argument it generates 1 through N; with two arguments, start through end; with three arguments, start, increment, end. The default separator is a newline, which can be changed with the -s option.
Retry logic often uses exponential backoff — doubling the wait time on each attempt. This can be implemented by combining sleep with arithmetic expansion: sleep $((INTERVAL * i)).
Common Mistakes
Common Mistake 1: macOS sleep does not support decimals
GNU coreutils' sleep on Linux accepts decimal seconds, but the standard macOS sleep (BSD sleep) only accepts whole-number seconds. Passing a decimal causes an error.
sleep 0.5 illegal time interval -- 0.5
To use sub-second sleep on macOS, install GNU coreutils via Homebrew and use gsleep.
brew install coreutils gsleep 0.5 && echo "Waited 0.5 seconds" Waited 0.5 seconds
When a script needs to run on both macOS and Linux, a common approach is to use gsleep if it is available and fall back to sleep otherwise.
cross_platform_sleep.sh
#!/bin/bash
SLEEP_CMD="sleep"
if command -v gsleep > /dev/null 2>&1; then
SLEEP_CMD="gsleep"
fi
$SLEEP_CMD 0.5
echo "Wait complete"
Common Mistake 2: Using a variable inside brace expansion
Brace expansion is processed before variable substitution in the shell's expansion order. As a result, writing {1..$MAX} does not expand the variable — the entire string is output literally.
MAX=5
echo {1..$MAX}
{1..5}
When the endpoint comes from a variable, seq or eval can be used. Since eval carries security risks, using seq is a common choice.
MAX=5 for i in $(seq 1 $MAX); do echo $i; done 1 2 3 4 5
If you find any errors or copyright issues, please contact us.