0

This is working as expected:

$ cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s
$ "[OUTPUT]\n    Name               cloudwatch_logs\n    Match              *\n    region             eu-central-1\n    log_group_name     TFE-LogForwarding\n    log_stream_name    TFE-AllLogs"

However, assignment to a variable does not work:

$ MY_VARIABLE=$(cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s)
$ echo $MY_VARIABLE 
jq - commandline JSON processor [version 1.5]
Usage: jq [options] <jq filter> [file...]

        jq is a tool for processing JSON inputs, applying the
        given filter to its JSON text inputs and producing the
        filter's results as JSON on standard output.
        The simplest filter is ., which is the identity filter,
        copying jq's input to its output unmodified (except for
        formatting).
        For more advanced filters see the jq(1) manpage ("man jq")
        and/or https://stedolan.github.io/jq

        Some of the options include:
         -c             compact instead of pretty-printed output;
       .... trimmed

I am on AWS EC2 machine with the latest Amazon Linux 2 image.

What is going on here?

The file looks like this:

[OUTPUT]
    Name               cloudwatch_logs
    Match              *
    region             eu-central-1
    log_group_name     TFE-LogForwarding
    log_stream_name    TFE-AllLogs
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
WorkoutBuddy
  • 609
  • 9
  • 23
  • 1
    Since the output contains space, you'll need to quote it: `MY_VARIABLE="$(cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s)"` – 0stone0 Sep 15 '21 at 12:02
  • same issue unfortunately. – WorkoutBuddy Sep 15 '21 at 12:04
  • Please share an example of the input file, so we can try it ourselfs. – 0stone0 Sep 15 '21 at 12:05
  • @0stone0 Right-hand side of an assignment doesn't undergo word splitting and doesn't need quotes, unless it's a string like `a='has space'`. Command substitution never requires quotes on a right-hand side. – Benjamin W. Sep 15 '21 at 12:09

1 Answers1

1

Two things:

  • You have to specify a filter for jq – just . to get the entire input
  • Once in a variable with whitespace, you must quote the string, else it shows up differently when you print it
var=$(cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s '.')
echo "$var" 

Relevant Q&A:

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116