Issues with Bash variable assignment

Hello, I am having an issue with a BASH shell.
I assign a variable with this code:
SERVICE_ID_EXISTS=$($PSQL “SELECT name FROM services WHERE service_id = $SERVICE_ID_WANTED”)

Output Looks like this, and it should read testTrim:
test Trim

If I print this, it has a leading white space, and as such I cannot test in an if [[ -z ]].
Why is bash attaching a leading whitespace and how do I fix it?

One initial thought… maybe the field you are querying has the space? (So not bash’s fault).

You can trim the spaces though…

Edit: reference for trimming https://www.postgresqltutorial.com/postgresql-trim-function/

1 Like

Thank you! When I test the length of the variable pulled from the SQL query, it shows the proper length, it happens only AFTER assigned to the bash variable. Have you seen that before?

my experience with bash is limited so I cannot say yes or no and have it mean anything worth your time.

Why don’t you double check the value in the table by going through the actual terminal to the database (connect to it) and running the select there? I just want you to be sure, really sure, it isn’t coming from the table data itself.

1 Like