shebang not working to run bash scripts in linux

眉间皱痕 提交于 2019-12-20 07:40:57

问题


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:


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.



来源:https://stackoverflow.com/questions/50702213/shebang-not-working-to-run-bash-scripts-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!