shebang not working to run bash scripts in linux

2020-05-09 18:58发布

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 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.

1条回答
家丑人穷心不美
2楼-- · 2020-05-09 19:31
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.

查看更多
登录 后发表回答