shebang无法在Linux中运行bash脚本

shebang无法在Linux中运行bash脚本

本文介绍了shebang无法在Linux中运行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法通过shebang将bash脚本转换为可执行文件.我的代码看起来像

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

#!/bin/bash
echo "hello"

该文件位于名为 test.sh 的文件中.我正在尝试让它与命令一起运行

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

代码正确输出

hello

我已经尝试过命令

which bash

返回目录/bin/bash 的目录,并且我也将此路径导出到 .bashrc 中,但无济于事.任何想法将不胜感激谢谢!为了清楚起见,我正在运行Linux Mint.

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.

推荐答案

chmod +x test.sh

设置可执行位正是需要的.脚本需要同时具有shebang行可执行权限.否则,您必须使用bash test.sh显式调用Shell.可执行位允许您编写./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.

这篇关于shebang无法在Linux中运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:10