Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace characters in Zsh variable

Tags:

zsh

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 ~ % 
like image 433
Stat.Enthus Avatar asked Dec 30 '25 16:12

Stat.Enthus


2 Answers

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.

like image 168
Paul Rooney Avatar answered Jan 05 '26 06:01

Paul Rooney


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
like image 38
philiptomk Avatar answered Jan 05 '26 07:01

philiptomk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!