Why calling a script by "scriptName" doesn't work?

bash, linux, macos

Solution

Remove the `-f` from your `#!/bin/bash -f` line.

`-f` prevents pathname expansion, which means that `*.cmake` will not match anything. When you run your script as a script, it interprets the shebang line, and in effect runs `/bin/bash -f scriptname`. When you run it as `. scriptname`, the shebang is just seen as a comment line and ignored, so the fact that you do not have `-f` set in your current environment allows it to work as expected.

Problem

I have a simple script `cmakeclean` to clean cmake temp files: ``` #!/bin/bash -f rm CMakeCache.txt rm *.cmake ``` which I call like ``` $ cmakeclean ``` And it does remove CMakeCache.txt, but it doesn't remove `cmake_install.cmake`: ``` rm: *.cmake: No such file or directory ``` When I run it like: ``` $ . cmakeclean ``` it does remove both. What is the difference and can I make this script work like an usual linux command (without `.` in front)? P.S. I am sure the both times is same script is executed. To check this I added `echo meme` in the script and rerun it in both ways.

Original source