我是 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
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
使用 bash 和参数扩展:
echo "$mod${str// / $mod}"
Run Code Online (Sandbox Code Playgroud)
输出:
-valThis -valIs -valMy -valBash -valString