-1

I can't seem to get bash scripts to turn into executable files via shebang. My code looks like

#!/bin/bash
echo "hello"

where this is in a file called test.sh. I'm trying to get it to run with the command

./test.sh

in the command line but i just receive the error of permission denied. When i change it to

sudo ./test.sh

I just get back that command not found. I can turn the file into an executable via the command the command line:

chmod +x test.sh

and the code correctly outputs

hello

I've tried the commands

which bash

which returned the directory /bin/bash and I've also exported this path in my .bashrc to no avail. Any ideas would be greatly appreciated thanks! I'm running Linux mint just for clarity.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133

1 Answers1

0
chmod +x test.sh

Setting the executable bit is exactly what's needed. A script needs both a shebang line and executable permission to be run. Otherwise you have to invoke a shell explicitly with, say, bash test.sh. The executable bit lets you write ./test.sh.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • the script works without the shebang though if i do the chmod + test.sh. So whats the purpose of the shebang then? – Anthony Kalaitzis Jun 05 '18 at 14:28
  • It's redundant if the shebang is #!/bin/bash and your interpreter is bash. But if the shebang is "#!/usr/bin/env python" and the script is written in python, or maybe if the script gets run in a shell other than bash (rare I suppose) it makes a lot of difference. – poleguy Nov 03 '21 at 14:05