I am trying to replace all the - chars with _ chars in a specific variable. Tried to use the tr function. What am I missing?
Thanks!
user@mbp-user ~ % echo $APP_ID
app1_someinfo_info-text_text-indfo_text
user@mbp-user ~ % APP_ID= $APP_ID tr - _
zsh: command not found: app1_someinfo_info-text_text-indfo_text
user@mbp-user ~ % APP_ID= $APP_ID tr "-" "_"
zsh: command not found: app1_someinfo_info-text_text-indfo_text
user@mbp-user ~ %
You can do this without invoking any other processes.
$ APP_ID=app1_someinfo_info-text_text-indfo_text
$ echo $APP_ID
app1_someinfo_info-text_text-indfo_text
$ echo ${APP_ID//-/_}
app1_someinfo_info_text_text_indfo_text
Or reassign to the same variable
$ APP_ID=${APP_ID//-/_}
Specifically we are using the pattern
name//pattern/string
which replaces all occurrences of pattern with string in the variable name.
Try the following:
[user@host ~]$ APP_ID="app1_someinfo_info-text_text-indfo_text"
[user@host ~]$ APP_ID=$(echo $APP_ID | tr "-" "_")
[user@host ~]$ echo $APP_ID
app1_someinfo_info_text_text_indfo_text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With