shell – Removing leading \n while assaigning to a variable – Education Career Blog

I have a DB2 query in a shell script which return an integer value, but I am unable to store it in a variable.

temp1= echo db2 -x "select max(id) from work.work_tb"

I am getting this output when I run it, sh -x test.sh

  • db2 -x select max(id) from work.work_tb
  • echo 50
  • temp1=
    50

So for some reason $temp1 is unable to get the value, I think its because the db2 query is returning value prefixed with \n. How do I get rid of the newline char and get the value to temp1?

,

No, that’s not why.

temp1=`db2 -x "select max(id) from work.work_tb"`

,

emp1=$(echo db2 -x "select max(id) from work.work_tb")

or using backticks

emp1=`echo db2 -x "select max(id) from work.work_tb"`

In general, to remove newlines, you can pass it to tools like tr/sed etc

... | tr -d "\n"

Leave a Comment