I run this zsh chained command in my terminal:

cd /path/to/backups/pod3 && rm -rf ./*(Om[1,-10])

This works fine and removes the appropriate directories as expected.

However, when I put that in a script, I get this error:

syntax error near unexpected token `('

I did some research and it seems I need to set this in my shell script:

setopt extended_glob

However, after I add that line at the top of the script, it throws its own error.

setopt: command not found

So I am not sure what I am doing wrong here and how to fix this.


You need to add a she-bang to your script to tell the system the script has to be interpreted by zsh, as otherwise it would be interpreted by sh instead:

#! /bin/zsh -
cd /path/to/backups/pod3 && rm -rf ./*(Om[1,-10])

Change /bin/zsh to the actual path of the zsh executable, or change the whole she-bang line to:

#! /usr/bin/env zsh

for the path of zsh to be looked up in $PATH (assuming the env command is in /usr/bin which is the case on most systems).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.