Pregunta

To replace substring in the bash string str I use:

str=${str/$pattern/$new}

However, I'm presently writing a script which will be executed with ash.

I have a string containing '/' and I want to use the above syntax inorder to replace the '/' in my string but it does not work.

I tried:

str=${str///a}
str=${str/\//a}
str=${str/'/'/a}

But they do not work

How I can fix that?

¿Fue útil?

Solución

This parameter expansion is a bash extension to POSIX sh. If you review the relevant section of IEEE standard 1003.1, you'll see that it isn't a required feature, so shells which promise only POSIX compliance, such as ash, have no obligation to implement it, and no obligation for their implementations to hew to any particular standard of correctness should they do so anyhow..

If you want bash extensions, you need to use bash (or other ksh derivatives which are extended similarly).

In the interim, you can use other tools. For instance:

str=$(printf '%s' "$str" | tr '/' 'a')

or

str=$(printf '%s' "$str" | sed -e 's@/@a@g')
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top