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
$ mkdir -p ~/scripts $ cd ~/scripts
Use a project directory instead when the script belongs to an application, deployment, or team repository.
#!/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.
$ 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
$ zsh hello-user.zsh Ops Hello, Ops
$ 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.
$ ./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.
$ 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.
$ rm hello-user.zsh