0

I try to process the result of the command in a loop, but the result of that command is unexpected. Instead of a string, a numeric value is obtained. Any ideas why it's happening?

$ psql -qAtX -c 'select json_agg(r) from (select client_addr as "{#ADDR}",application_name as "{#NAME}",sync_state as "{#STATE}" from pg_stat_replication where usename<>'\''backup'\'') r' > /tmp/out.txt
$ cat /tmp/out.txt
[{"{#ADDR}":"192.168.0.100","{#NAME}":"test","{#STATE}":"async"}]
$ for i in $(< /tmp/out.txt); do echo $i;done
1
  • 1
    Do you have a file named "1" in the current directory? – glenn jackman May 02 '22 at 01:22
  • 1
    Since the variable `$i` is unquoted, after parameter substitutions, the value is subject to word splitting and filename expansion. Due to the square brackets, that text is equivalent to the glob pattern `["#,.012689:ADEMNRSTacensty{}]`, and can match any corresponding 1-character filenames. See [3.5 Shell Expansions](https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions) in the manual. – glenn jackman May 02 '22 at 01:26
  • @glennjackman, deleting file with name "1" solved this problem – алексей иванов May 02 '22 at 03:38
  • 1
    This is one of many reasons to double-quote your variable references. See ["I just assigned a variable, but `echo $variable` shows something else"](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Gordon Davisson May 02 '22 at 03:39
  • The actual solution will be `echo "$i"` – glenn jackman May 02 '22 at 14:29

0 Answers0