bash - 在bash中,将字符串的每个字符替换为固定字符 ("blank out"一个字符串)

标签 bash parameter-expansion shellcheck

MWE:

caption()
{
    echo "$@"
    echo "$@" | sed 's/./-/g'  # -> SC2001
}

caption "$@"

我想使用参数扩展来消除 shellcheck 错误。我最好的想法是使用 echo "${@//?/-}",但这并不能取代空格。

有什么想法吗?

最佳答案

您可以使用$*将其保存在局部变量中:

caption() {
   local s="$*"
   printf '%s\n' "$s" "${s//?/-}"
}

测试:

caption 'SC 2001' foo bar

SC 2001 foo bar
---------------

关于bash - 在bash中,将字符串的每个字符替换为固定字符 ("blank out"一个字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72230619/

相关文章:

linux - Shell如何杀死一个进程并返回0

bash - 负匹配在参数扩展中extglob是如何工作的

bash - 使用纯 bash 创建 URL 友好的 slug?

bash - ShellCheck 警告 : "Iterating over ls output is fragile. Use globs. [SC2045]"

bash - 是否可以在使用位置变量时解析 SC2001 ("See if you can use ${variable//search/replace} instead")?

bash - 从每个参数中删除尾部斜杠的最简单方法是什么?

bash - 设置 errexit 选项时检测 shell 退出代码的正确方法是什么?

bash - 如何在 grep 中为新行提供模式?

bash - ${v^^} 和 ${v@U} 的区别?

bash - 这个错误是什么意思? (SC2129 : Consider using { cmd1; cmd2; } >> file instead of individual redirects.)