不回显读取输入不起作用(bash Shell 脚本)

Read input without echoing is not working (bash Shell Script)

“-s”开关不工作 bash shell 脚本。它在不提示输入密码的情况下抛出错误。

使用:Ubuntu 16.04.6 LTS

我错过了什么吗?

#!/bin/bash

echo "Enter password"
read -s password
echo "password:" $password

输入密码 test.sh: 4: 读取:非法选项 -s 密码:

您是 运行 具有 sh 的脚本,而不是 bash 提供相关功能的脚本。使用 bash:

直接调用脚本
bash test.sh

或者使文件可执行,以便解释器读取您的 shebang:

chmod +x test.sh    # only required once
./test.sh

.sh 扩展是不必要的,除非你是 运行 某些特殊目录中的实际 POSIX shell 脚本。