Question

I need change a string before printing in bash. So I did:

${RETEZEC//./D}

Now I need to change one more thing, - for _. But no idea how to add it there? In the same step something like $`{RETEZEC//./D;//-/_} and i need one extra exeption for one - ?

Was it helpful?

Solution 2

It's not possible to nest parameter expansion, you need two expressions:

foo=${RETEZEC//./D}
echo "${foo//-/_}"

You can, however, use an external tool to do this but I would recommend to stick with the native shell solution above.

$ echo "foo.-bar" | tr '.-' 'D_'
fooD-bar

OTHER TIPS

It looks like you want to replace . with D and - with _.

As others comment, you cannot do it in just one step. But you can use tr:

$ echo "$RETEZEC"
hel-lo_a.b.
$ tr '.-' 'D_' <<< "$RETEZEC"
hel_lo_aDbD

If you want to store the output in the same variable, do:

$ RETEZEC=$(tr '.-' 'D_' <<< "$RETEZEC")
$ echo $RETEZEC
hel_lo_aDbD

You can use:

s='abc-def-foo'
echo "${s//-/_}"
abc_def_foo
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top