0

I wrote a simple bash script to create some directories, like so:

#!/bin/sh
PROJECT_DIR=$(cd "$(dirname "$0")" && pwd)
cd ${PROJECT_DIR} || exit 1

mkdir -p Website/{static/{cs,js},templates/{html,xhtml}}

However, after I run the script (./script.sh), the directory structure looks like this:

enter image description here

There is no syntax error in my script. When I try the mkdir command directly on the terminal, the directories are created correctly.

Why is the bash script run behaving this way?

Snow
  • 1,058
  • 2
  • 19
  • 47
  • 6
    Use a `bash` shebang to get `bash` features. – Mark Setchell Jun 27 '22 at 15:20
  • 3
    Just to clarify what @MarkSetchell said: because your script starts with `#!/bin/sh` rather than `#!/bin/bash`, it is apparently executing a non-Bash shell. If you're using bash-specific features, be explicit and always use `#!/bin/bash`. While `/bin/sh` may be bash on some distributions, that's not always the case. – larsks Jun 27 '22 at 15:21
  • 2
    Paste your script into shellcheck.net it will tell you the problem – Barmar Jun 27 '22 at 15:23
  • @larsks Even when it's a link to `bash` it disables most extensions. – Barmar Jun 27 '22 at 15:24
  • @MarkSetchell @larsks Ah I see, I'll make sure to use `#!/bin/bash` or `#!/usr/bin/env bash` next time, thanks! The script now runs correctly. – Snow Jun 27 '22 at 15:26
  • Does this answer your question? [Difference between sh and Bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – wjandrea Jun 27 '22 at 19:53

0 Answers0