问题
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