Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. sleep / seq

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

UsageDescription
sleep 5Wait 5 seconds. No unit defaults to seconds.
sleep 0.5Wait 0.5 seconds (500 ms). macOS's built-in sleep does not support decimals (see below).
sleep 1mWait 1 minute (60 seconds).
sleep 1hWait 1 hour.
sleep 1dWait 1 day (24 hours).

seq Options

OptionDescription
-wPad numbers with leading zeros to equalize width (01, 02, ... 10).
-s stringUse the specified string as the separator. Defaults to newline.
-f formatUse 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 supportYesNo (literals only)
Custom incrementYes (seq 1 2 10)Yes ({1..10..2})
Zero paddingYes (-w option)Yes ({01..10})
External commandRequiredNot required (Bash built-in)
Decimal sequencesYes (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 .