0

I am trying to replace all white spaces with _. I used the following code:

FONT="DejaVu Sans Mono"
FONT_CODE=${FONT//[ ]/_}
echo $FONT_CODE 

I'm expecting DejaVu_Sans_Mono as the output But I got the following error :

x.sh: 2: Bad substitution

I am not sure what I need to do to get work.

AKMalkadi
  • 341

1 Answers1

0

Here is how I solved my issue after getting hints from the comments. I used zsh instead of sh and it worked for me.

First, I had to install zsh:

sudo apt install zsh

Then, I used zsh instead of sh in the terminal:

zsh x.sh

I got no error and this is the output:

DejaVu_Sans_Mono

AKMalkadi
  • 341
  • To make it work in sh, just do printf '%s' "$FONT" | tr ' ' '_'. – Kusalananda Jun 03 '21 at 17:11
  • @Kusalananda Well, that's in case if you want to print immediately. What about if you want to assign it to a variable? – AKMalkadi Jun 03 '21 at 20:27
  • Use an ordinary command substitution: variable=$( printf '%s' "$FONT" | tr ' ' '_' ) I'm also noting that the code in your question ends with outputting the modified value, so it shouldn't be necessary to store it in an intermediate variable. – Kusalananda Jun 03 '21 at 20:27