I'm very new to scripting. I would like to add the output of the sql function given below to another variable for example:

month_end ()
mdate=$($SQLPLUS_HOME/sqlplus.exe -S $DBC  << END
set pagesize 0 feedback off verify off heading off echo off;
select to_char(last_day(add_months(sysdate,-1)),'yyyymmdd') from dual;

when I echo the output from $mdate is perfect. (20160531)

But when I append to another varable, the whole value is broken ( _WORLD.txt instead of Hello_20160531_WORLD.txt).


echo ${NAME}
  • you need to quote your variables. e.g. NAME="$FILENAME_${mdate}_${EXTENSION}" and echo "$NAME" – cas Jun 7 '16 at 2:34

That's because underscore _ is a valid part of a variable name, so you also need the curly braces around FILENAME:


Based on your other response, it appears that you also have a carriage return (\r) in either the FILENAME assignment line or the output of sqlplus, in $mdate. Find out which by running cat -v on your shell script, and/or doing echo $mdate | cat -v. Since I just noticed the sqlplus.exe, I assume you're running in a Cygwin-like environment, which may be adding the carriage return. Fix it by adding mdate=${mdate//^M/} where you enter ^M by typing control-v, control-m.

  • Sorry, I'm new to this forum as well. – Rachel Jun 6 '16 at 14:06
  • thanks for your immediate response, I just tried your sugeestion, but am getting another broken output (_WORLD.txt0531) Please let me know whats happening and how I can solve this problem. Thanks, – Rachel Jun 6 '16 at 14:07
  • Hi Jeff, I tried to work with your solution. But when I'm tryig to use ctrl v and ctrl m, it is treating ctrl v as paste option and ctrl m as new line. So I'm not getting expected result. Am I doing something wrong? Please help me.. – Rachel Jun 14 '16 at 10:52
  • in my case, I have outputed the value to a text file, trimed the carraige return '\r' with tr -d command andwritten to another variable – Rachel Sep 16 '16 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.