修改bash字符串

我是 bash 新手,需要帮助解决这个问题。

#!/bin/bash

str="This Is My Bash String"
mod="-val"

# Loop through characters of str. If index is 0, insert mod at position 0. And if the current index contains space (" "), then insert mod after the space.

echo -e str
# This should output: -valThis -valIs -valMy -valBash -valString

我怎样才能做到这一点?

回答

使用 bash 和参数扩展:

echo "$mod${str// / $mod}"

输出:

-valThis -valIs -valMy -valBash -valString


以上是修改bash字符串的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>