Home > OS >  Bash echo is changing two spaces into tabulator if string contains this *,*
Bash echo is changing two spaces into tabulator if string contains this *,*

Time:07-16

I have a bash script that takes data from an Oracle database and prints them. (my script is bit more complicated then that but I cut the parts that are not important is this case).

So in Oracle DB I have a table

CREATE TABLE spacetest (
col1 varchar2(20),
col2 varchar2(100)
);
INSERT INTO spacetest values('TEST','BETWEEN  TWOSPACES');
commit;

Here I have this string 'BETWEEN TWOSPACES' where I literally have two space, ASCII 32enter image description here

If I query the table I have two spaces in the string.

Now if I run a bash script

#!/bin/bash
set -x
  lsout=$(sqlplus -S /NOLOG <<EOF
set heading off termout off pagesize 0 echo off
conn user/password@//localhost:1521/localdatabase
SELECT * FROM spacetest;
select COL1 || '*,*' || COL2 || '#@' combined FROM SPACETEST;
select COL1 || '*,' ||  COL2 || '#@' combined FROM SPACETEST;
select COL1 || ',*' ||  COL2 || '#@' combined FROM SPACETEST;
select COL1 || '**' ||  COL2 || '#@' combined FROM SPACETEST;
select COL1 || '°,#' ||  COL2 || '#@' combined FROM SPACETEST;
select COL1 || '*#*' || COL2 || '#@' combined FROM SPACETEST;
exit
EOF
)
echo "$lsout" | od -c

echo done

Those containing *,* or #,# have the spaces in the string converted into tabulators. This behavior doesn't occur in database, there all the results contains two spaces. enter image description here

Any idea for this behavior?

SELECT * FROM v$version;
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0

$echo "${BASH_VERSION}" 
4.2.46(2)-release

NOTE: I edited my former question, because I was able to simplify it and cut the pl/sql part of it.

CodePudding user response:

The tabs were being inserted by the sqlplus command, and the solution was to add SET TAB OFF to the SQL commands. I found this in the Oracle SQL*Plus documentation:

SET TAB {ON|OFF}
SET TAB is not supported in iSQL*Plus
Determines how SQL*Plus formats white space in terminal output. OFF uses spaces to format white space in the output. ON uses the TAB character. TAB settings are every eight characters. The default value for TAB is system dependent.

Apparently the default value is OFF in SQL Developer, but ON in the command-line sqlplus program. I'm still not clear why the tab conversion only happened for some outputs.

  • Related