bash:查找解释器的名称

标签 bash

我有一个字符串,其中包含脚本中第一行的内容,例如

#!/bin/bash

但它可以是任何东西,例如

#!/path/to/another/interpret -some args -another arg

如何通过省略路径和参数来提取解释器的名称 - “bash”、“interpret”?

我想找到第一个空格来将参数与路径分开。

你能帮忙吗?

最佳答案

我认为你不能在 shebang 中使用空格。如果找不到,您可以像这样找到解释器名称:

interpreter=`cat test.sh | head -n 1 | cut -d' ' -f 1`
basename $interpreter

编辑: 缩短版本:

interpreter=$(basename $(sed '2q;s/^#!//;s/ .*//' test.sh))

关于bash:查找解释器的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859244/

相关文章:

bash - #! 脚本可以用作解释器吗?散列线?

linux - 打开 gnome-terminal 的多个选项卡,每个触发命令延迟 N 秒

linux - 如何在句子之间进行比较并计算相似度?

arrays - 根据月份确定冬季或夏季时间(UTC 时区)

bash - 使用 AWK 中的第一个字段作为文件名

bash - 将空终止字符串传递给 bash 函数

bash - 从Docker容器A(在Docker容器B上)运行Bash脚本

linux - wget 使用环境变量

linux - 如何替换占位符的值

c++ - 是否建议在 bash 脚本中捕获 SIGPIPE?