0

I am trying capture command output to a variable but asterisk(*) characters automatically gets converted into directory files.

Command I want to run

secrets=$(vault read kv/data/secrets)

Expression to recreate the scenario

cronExpression=$(echo "33 * * *") && echo $cronExpression

Expected

cronExpression="33 * * *"

Actual

33 LICENSE README.md test.json LICENSE README.md test.json LICENSE README.md test.json

I have LICENSE README.md test.json files in a folder where I am trying to execute this command so each * gets converted into list files in the same directory

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71

1 Answers1

1

Like this:

cronExpression="$(echo "33 * * *")" && echo "$cronExpression"
33 * * *

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
when-is-double-quoting-necessary

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223