YihanBao
YihanBao

Reputation: 563

Bash scripts return a substring not whole value in column for psql

I want to get the value of message in db, which for example is "the error is missing index " but when I run the code belong, it only return me 'the' for error_type. How could this happen that only return me the first word but not the whole value of the query result? How can I get the query results with the whole value?

declare -a ROW=($(psql \
    -X \
    -U $DB_USER \
    -h $DB_HOST \
    -d $DB_NAME \
    -p $DB_PORT \
    --single-transaction \
    --set AUTOCOMMIT=off \
    --set ON_ERROR_STOP=on \
    --no-align \
    -t \
    --field-separator ' ' \
    --quiet \
    -c "SELECT message
        FROM error_message
        WHERE created_at > '$t1' and created_at < '$t'")
)

error_type=${ROW[0]}
echo $error_type

Upvotes: 0

Views: 74

Answers (1)

Barmar
Barmar

Reputation: 781721

Don't use an array, use an ordinary string variable to contain the whole result.

error_type=$(psql \
    -X \
    -U $DB_USER \
    -h $DB_HOST \
    -d $DB_NAME \
    -p $DB_PORT \
    --single-transaction \
    --set AUTOCOMMIT=off \
    --set ON_ERROR_STOP=on \
    --no-align \
    -t \
    --field-separator ' ' \
    --quiet \
    -c "SELECT message
        FROM error_message
        WHERE created_at > '$t1' and created_at < '$t'")

echo "$error_type"

Remember to quote variables unless you need word splitting and wildcard expansion to be done.

Upvotes: 1

Related Questions