How to create and run a Zsh script

Creating a Zsh script turns repeatable terminal commands into a file that can be checked, run through Zsh, and launched directly after the executable bit is set. A working script needs a shebang line, a local option baseline, and output that proves the file received the expected argument.

The first line, #!/usr/bin/env zsh, tells the operating system to find Zsh when the file is executed as a command. Running zsh -n checks the script syntax without executing the body, while chmod u+x gives the file owner permission to launch it through ./scriptname.

The sample creates a small greeting script in a local scripts directory. It runs once by passing the file to Zsh, then runs again through the executable path so both interpreter execution and direct execution are proven before the script is kept or reused. The final permission check ties the direct run to the owner execute bit instead of only to the script contents.

Steps to create and run a Zsh script:

  1. Create a directory for local shell scripts and move into it.
    $ mkdir -p ~/scripts
    $ cd ~/scripts

    Use a project directory instead when the script belongs to an application, deployment, or team repository.

  2. Create the script file with a Zsh shebang and a simple argument fallback.
    ~/scripts/hello-user.zsh
    #!/usr/bin/env zsh
    emulate -L zsh
    setopt err_exit no_unset
     
    name=${1:-operator}
    print -r -- "Hello, $name"

    emulate -L zsh sets a Zsh option baseline before stricter options are enabled. err_exit stops the script after an unhandled command failure, and no_unset reports unset parameters instead of expanding them silently.

  3. Check the script syntax before running it.
    $ zsh -n hello-user.zsh

    No output from zsh -n means Zsh parsed the file without finding a syntax error.

  4. Run the script by passing it directly to Zsh.
    $ zsh hello-user.zsh Ops
    Hello, Ops
  5. Mark the script executable for the file owner.
    $ chmod u+x hello-user.zsh

    The u+x mode adds owner execute permission and leaves the existing group and other permissions unchanged. Use a stricter mode such as chmod 700 hello-user.zsh when the script contains private commands or data.

    Tool: chmod Calculator can compare symbolic and numeric modes before using a stricter permission.

  6. Run the script through its executable path.
    $ ./hello-user.zsh Backup
    Hello, Backup

    Direct execution uses the #!/usr/bin/env zsh line, so the command succeeds only when Zsh is available through the current PATH.

  7. Confirm that the owner permission triplet includes execute permission.
    $ ls -l hello-user.zsh
    -rwxr--r-- 1 operator operator 107 Jun  5 08:34 hello-user.zsh

    The key signal after chmod u+x is the x in the owner permission triplet. Group and other bits still reflect the file's original mode and the shell's default file mask.

  8. Keep the script in the scripts directory or move it to a directory that is already in PATH after the direct run and permission check work.
  9. Remove the sample script when it was created only for testing.
    $ rm hello-user.zsh