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.
Related: How to set the PATH environment variable in Zsh
Related: How to debug a Zsh script
Related: How to create a function in Zsh
Related: Create and run a Bash script
Steps to create and run a Zsh script:
- 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.
- 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.
- 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.
Related: How to debug a Zsh script
- Run the script by passing it directly to Zsh.
$ zsh hello-user.zsh Ops Hello, Ops
- 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.
- 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.
- 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.
- 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.
- Remove the sample script when it was created only for testing.
$ rm hello-user.zsh
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.