1

I'm new to shell scripting and generally the usage of Terminal. I recently made a script.sh that I want to use by just typing its name without the full path. So, I wrote the path of the directory into .zprofile as:

export "PATH=$PATH:/path/to/scriptdirectory"

and changed the file permissions with:

% chmod +x script.sh

If I use the script with:

% sh myscript.sh

everything run smoothly as intended. But if I try to omit the sh and type:

% myscript.sh

the script start running its first part, which is just reading a variable, but as soon as I insert the value, the follow line pops:

/path/to/scriptdirectory/script.sh:11: = not found

This is the script:

#!/bin/zsh
echo "Do you want to set or change the backup location? (type y/n)"
read SET_BACKUP
while [ "$SET_BACKUP" != "y" ] && [ "$SET_BACKUP" != "n" ]
do
    echo "Please, write y or n:"
    read SET_BACKUP
done
if [ "$SET_BACKUP" == "y" ]
then
    echo "Write the absolute path of the desired backup location:"
    read BACKUP_FOLDER
    echo "Starting backup..."
    cp -R "/fileslocation" "$BACKUP_FOLDER"
    echo "Backup completed."
else
    echo "Starting backup..."
    cp -R "/fileslocation" "$BACKUP_FOLDER"
    echo "Backup completed."
fi
5
  • 2
    "At line 11 there's [...], so I think it's referring to that." -- What can we tell without seeing the script? Please edit the question and post the script, or at least a fragment near the line where the problem seems to be. Commented Jun 23, 2022 at 11:45
  • In addition to what @KamilMaciorowski says, you make no mention of your shebang so we (and possibly you) have NO WAY to know what shell is interpreting your script. When you did sh thingy.sh it implies that sh (the bourne shell) is executing your script and not bash or zsh. Without it (and no shebang) you are using the system default (probably bash). These behave differently. Commented Jun 23, 2022 at 13:16
  • I just edited the question and posted the whole script, thank you for the patience. I used #!/bin/zsh as shebang.
    – comws
    Commented Jun 23, 2022 at 13:32
  • What happens when you run it like zsh script.sh? Do you get the same error? Which line emits the error? I don't see a = around the 11th line Commented Jun 23, 2022 at 14:00
  • @glennjackman Everything's fine, the answer below is what I needed, thank you anyway. I have to use "[[" instead of "[" for zsh.
    – comws
    Commented Jun 24, 2022 at 14:00

2 Answers 2

0

The difference is that you're running the script with two different shell interpreters.

When you use sh myscript.sh, you're running /bin/sh and not zsh. When sh runs it reads your script myscript.sh and uses sh/bash shell rules for its syntax.

When you use just myscript.sh, the command used to run the script is determined by the first line: #!/bin/zsh (also known as the shebang). So it's run using zsh and uses its rules for syntax.

I see two solutions to get this working:

  1. change the first line to use bash instead, so it'd be #!/bin/bash
  2. read more about how conditionals work in zsh and fix the line that's broken (it looks like it's the line with if [ "$SET_BACKUP" == "y" ])
1
  • 1
    Thank you so much, I just found about the issue with using "==" in zsh! Now everything goes as intended.
    – comws
    Commented Jun 23, 2022 at 13:46
0

This is not really an answer, more of a formatted question.

3 things come to mind:

  1. variable assignments must not have spaces around the =: answer=42
  2. $ is only used to de-reference a variable, not to assign: don't do $answer=42
  3. quote your variables: if [ "$answer" = 42 ]

This is me trying to read your mind. Put some code in your question.

Also, use https://shellcheck.net to validate your shell code.

1
  • I just posted the whole script on the question, sorry for that. Spellcheck didn't find any issues, and I've already done all that you've suggested; also the script run completely fine if I use sh.
    – comws
    Commented Jun 23, 2022 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .