'In bash, replace each character of a string with a fixed character ("blank out" a string)
MWE:
caption()
{
echo "$@"
echo "$@" | sed 's/./-/g' # -> SC2001
}
caption "$@"
I'd like to use parameter expansion in order to get rid of the shellcheck error. The best idea I had was to use echo "${@//?/-}"
, but this does not replace spaces.
Any ideas?
Solution 1:[1]
You can use $*
to save it in a local variable:
caption() {
local s="$*"
printf '%s\n' "$s" "${s//?/-}"
}
Test:
caption 'SC 2001' foo bar
SC 2001 foo bar
---------------
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |