相关疑难解决方法(0)

"#!/ usr/bin/env bash"和"#!/ usr/bin/bash"有什么区别?

bash脚本的标题中,这两个语句之间的区别是什么?

  1. #!/usr/bin/env bash

  2. #!/usr/bin/bash

当我试图看到env手册页时,我得到了这个定义:

 env - run a program in a modified environment
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

unix linux bash shell shebang

319
推荐指数
4
解决办法
13万
查看次数

为什么#!/ usr/bin/env bash优于#!/ bin/bash?

我在很多地方看到过,包括对这个网站的推荐(什么是首选的Bash shebang?),#!/usr/bin/env bash优先使用#!/bin/bash.我还看过一个进取的个人建议使用#!/bin/bash错误的和bash的功能会这样做会丢失.

总而言之,我在严格控制的测试环境中使用bash,其中每个循环驱动器基本上都是单个主驱动器的克隆.我理解可移植性论点,虽然它不一定适用于我的情况.是否有任何其他理由更喜欢#!/usr/bin/env bash替代品,并且假设可移植性是一个问题,是否有任何理由使用它可能会破坏功能?

bash shebang

185
推荐指数
4
解决办法
6万
查看次数

有没有办法在Bash脚本中创建键值对?

我正在尝试使用Bash脚本创建键值对的字典.我正在尝试使用这个逻辑:

declare -d dictionary
defaults write "$dictionary" key -string "$value"
Run Code Online (Sandbox Code Playgroud)

... $dictionary变量在哪里,但这不起作用.

有没有办法在Bash脚本中创建键值对?

bash shell

67
推荐指数
3
解决办法
9万
查看次数

mac vscode bash: shopt: globstar: 无效的 shell 选项名称

在 Mac 上运行 vscode。我更新了 Mac bash 版本,正如您所看到的,内置的 vscode 终端显示的 bash 版本与 Mac 终端的版本相同。当我获取 .bash_profile 时,我invalid shell option name在 vscode 终端中收到错误。是什么原因造成的?

FWIW - 终端 > 集成 > Shell: Osx 设置为bin/bash,如果这不明显的话。而且,我的 SHELL 变量是相同的。

在此输入图像描述

macos bash visual-studio-code shopt

8
推荐指数
0
解决办法
6822
查看次数

Bash 5 - 找不到 mapfile / readarray 命令

我使用的是 MacOS Cagalina 10.15.4。我认为 mapfile 和 readarray 在 Bash 4 及更高版本中可用,但出现在下面。我可以问我做错了什么吗?

$ brew install bash
$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-apple-darwin19.4.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

$ mapfile
-bash: mapfile: command not found

$ readarray
-bash: readarray: command not found
Run Code Online (Sandbox Code Playgroud)

bash

6
推荐指数
0
解决办法
1万
查看次数

如何转义关联数组中具有连字符的键

我想在bash中创建一个地图,其中某些地图键的值可能包含连字符(-

我试过下面的代码

declare -a buckets
buckets["us-east-1"]="bucketname-us-east-1";

region="us-east-1"
buckets[$region]="bucketname-us-east-1"; 

# both of them throws buckets["us-east-2"]: bad array subscript

buckets["us\-east\-1"]="bucketname-us-east-1"; 
# throws syntax error: invalid arithmetic operator (error token is "\-east\-1")
Run Code Online (Sandbox Code Playgroud)

还有其他创建地图的方法吗?

linux bash dictionary

3
推荐指数
1
解决办法
98
查看次数

是否可以在不支持关联数组的情况下编译 Bash 5?

测试我的 bash 脚本的同事向我发送了此屏幕截图

没有关联数组的 ba​​sh5

bash从 brew安装。有可能declare没有-A选项bash:5吗?

我用 docker 检查过,bash:5必须有-A

$ docker run -it bash:5
bash-5.0# declare -A
declare -A BASH_ALIASES=()
declare -A BASH_CMDS=()
Run Code Online (Sandbox Code Playgroud)

bash:3当没有关联数组支持时,屏幕截图类似于输出:

$ docker run -it bash:3
bash-3.2# declare -A
bash: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
Run Code Online (Sandbox Code Playgroud)

是否可以在bash:5没有关联数组支持的情况下进行编译?

bash自制程序包的页面,我在./configureFormula ruby​​ 安装脚本部分没有找到任何特殊选项。

macos bash homebrew configure

1
推荐指数
1
解决办法
722
查看次数

即使在 MacOSX 中配置后,我也无法使用 bash 5 语法

我对 bash 比较陌生。

我的 mac 默认安装了 bash 3。

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)

我相信我已经为 MacOSX 安装并配置了 bash 5。运行bash -version命令输出:

GNU bash, version 5.0.3(1)-release (x86_64-apple-darwin18.2.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)

问题是我无法执行任何 Bash 5 功能,例如此代码使用 bash 4 功能无法输出预期行为:

for i …
Run Code Online (Sandbox Code Playgroud)

macos bash

1
推荐指数
1
解决办法
801
查看次数