Home > OS >  Why is my script not printing output on one line?
Why is my script not printing output on one line?

Time:12-28

This is an image of what I'm asking for I am using the following -echo- in a script and after I execute, the output format is as shown below:

`echo -e "UPDATE table1 SET table1_f1='$Fname' ,table1_f2='$Lname' where table1_f3='$id';\ncommit;" >> $OutputFile` 

output: UPDATE table1 SET table1_f1='Fname' ,table1_f2='Lname' where table1_f3='id '; the '; is appearing on a new line, why is that happening?

CodePudding user response:

The variable $id in your shell script actually contains that newline (\n or \r\n) at the end; so there isn't really anything wrong in the part of the script you've shown here.

This effect is pretty common if the variable is created based on external commands (update:) or by reading external files as you are here.

For simple values, one way to strip the newline off the end of the value, prior to using it in your echo is:

id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );

or for scripts that already rely on a particular bash IFS value:

OLDIFS="${IFS}"; 
IFS=$'\n\t '; 
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
IFS="${OLDIFS}";
  • Related