0

I am trying to pass a string-valued variable as a literal string in bash:

TEXT='Esto concuerda con la observación de Hord, que afirma que las espinacas contienen mucha vitamina K, lo que ayuda a reducir la presión arterial y el riesgo de sufrir enfermedades cardiovasculares.'

python normalize.py \
    --text $TEXT \
    --language es \
    --cache_dir ./es_norm_cache_dir/

The script normalize.py expects a string, so the above should expand to:

python normalize.py \
    --text 'Esto concuerda con la observación de Hord, que afirma que las espinacas contienen mucha vitamina K, lo que ayuda a reducir la presión arterial y el riesgo de sufrir enfermedades cardiovasculares.' \
    --language es \
    --cache_dir ./es_norm_cache_dir/

i.e. including the (non-escaping) single quotes. This second call runs as desired.

How can I use a variable but still make the above call equivalent to the second code block written above?

Anil
  • 1,097
  • 7
  • 20

1 Answers1

0

You can easily see the effect of putting double quotes around the parameter with printf.

Here "first_word" and "second_word" are seeing as two parameters:

$ printf "%s\n" first_word second_word
first_word
second_word

While here as a single parameter:

$ printf "%s\n" "first_word second_word"
first_word second_word

This distinction holds true even if there's a variable expansion inside:

$ VAR="first_word second_word"
$ printf "%s\n" "${VAR}"
first_word second_word

More of this here:

[...] the double quotes protect the value of each parameter (variable) from undergoing word splitting or globbing should it happen to contain whitespace or wildcard characters

Jardel Lucca
  • 1,115
  • 3
  • 10
  • 19
  • 1
    Please don't link the ABS -- it's notorious as a source of bad-practice examples and outdated information. The [Wooledge BashGuide](https://mywiki.wooledge.org/BashGuide) was written when the #bash IRC channel was tired of trying to get people to unlearn bad habits they picked up from the ABS, but there are plenty of other good resources as well -- see the [bash tag wiki](https://stackoverflow.com/tags/bash/info) for a list. – Charles Duffy Sep 12 '22 at 14:54
  • Thanks @CharlesDuffy! I've just done a quick search and confirmed this fame from ABS of having outdated info.! Will be more careful next time I see it... – Jardel Lucca Sep 12 '22 at 15:25