Unix/Linux Go Back    


UNIX and Linux Applications Discuss UNIX and Linux software applications. This includes SQL, Databases, Middleware, MOM, SOA, EDA, CEP, BI, BPM and similar topics.

Space truncation

UNIX and Linux Applications


Tags
solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-24-2015
dhilipans1988 dhilipans1988 is offline
Registered User
 
Join Date: Jul 2015
Last Activity: 5 October 2015, 7:28 AM EDT
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Space truncation

HI All-

We have script like the following
Code:
a='h1    '
b='12434    '
c='   fagkasdbkZ<M'

output=$a$b$c
echo $output > /home/dsomasun/fil_concat.txt

But in the output file spaces are truncated and Output is coming like the below
Code:
h1 1234 fagkasdbkZ<M

please advise


Moderator's Comments:
Space truncation
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier top read and preserves multiple spaces for indenting or fixed-width data

Last edited by rbatte1; 07-24-2015 at 12:30 PM.. Reason: CODE tags
Sponsored Links
    #2  
Old Unix and Linux 07-24-2015
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 22 January 2017, 11:23 AM EST
Location: Chennai
Posts: 2,357
Thanks: 517
Thanked 1,096 Times in 993 Posts
Hello dhilipans1988,

Welcome to forum, kindly use code tags for commands/inputs/codes which you are using in your posts as per forum rules. You should use printf for same because it is known behavior for echo.

Code:
singh_test@Singh$ a='h1 '
singh_test@Singh$ b='12434 '
singh_test@Singh$ c=' fagkasdbkZ<M'
singh_test@Singh$ printf "$a $b $c\n" > testpri
singh_test@Singh$ cat testpri
h1  12434   fagkasdbkZ<M

Hope this helps, enjoy learning and sharing knowledge in forumLinux

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
dhilipans1988 (07-24-2015)
Sponsored Links
    #3  
Old Unix and Linux 07-24-2015
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 January 2017, 12:42 PM EST
Location: Lancashire, UK
Posts: 2,928
Thanks: 1,205
Thanked 544 Times in 492 Posts
The difference is in the quoting. The shell will strip out multiple spaces.

Use echo "$a$b$c" instead.

Better still, you should also use {&} around variable names to clearly mark where they start and end and printf instead of echo so you would end up with:-
Code:
printf "${a}${b}${c}"

Unfortunately, echo can vary between vendors.



Robin
The Following User Says Thank You to rbatte1 For This Useful Post:
dhilipans1988 (07-24-2015)
    #4  
Old Unix and Linux 07-24-2015
dhilipans1988 dhilipans1988 is offline
Registered User
 
Join Date: Jul 2015
Last Activity: 5 October 2015, 7:28 AM EDT
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Thank you All.. It worked!!!!
Sponsored Links
    #5  
Old Unix and Linux 07-24-2015
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 January 2017, 3:24 AM EST
Location: San Jose, CA, USA
Posts: 9,862
Thanks: 476
Thanked 3,385 Times in 2,891 Posts
Quote:
Originally Posted by rbatte1 View Post
The difference is in the quoting. The shell will strip out multiple spaces.

Use echo "$a$b$c" instead.

Better still, you should also use {&} around variable names to clearly mark where they start and end and printf instead of echo so you would end up with:-
Code:
printf "${a}${b}${c}"

Unfortunately, echo can vary between vendors.



Robin
The echo is dangerous if any if the first variable expands to a string starting with a hyphen or if any of the variables expand to a string containing any backslash characters. Otherwise:

Code:
echo "$a$b$c"

is fine. But, if you're reading data from a user supplied source, never assume that you know what the data will contain, and, as Robin suggested, use printf instead.

But:

Code:
printf "${a}${b}${c}"

is also dangerous (and doesn't add the trailing <newline> character that echo would provide). The dangers are the same as with echo but has an additional problem if the expansion of any of those variables contain a <percent-sign> character. The safe way to print three variables (with no added space between them and with a trailing <newline>) would be:

Code:
printf '%s%s%s\n' "$a" "$b" "$c"

You only need to include braces around variable names if text following the variable name could be part of a valid variable name. For instance if you wanted to print the expansion of the variable b followed by the character b you could use either of the following:

Code:
printf '%sb\n' "$b"
printf '%s\n' "${b}b"

but not either of:

Code:
echo "$bb"
printf '%s\n' "$bb"

since both of those you result in the expansion of the variable bb instead of the variable b. Shell variable names can contain letters, digits, and underscores where the 1st character is not a digit.

Positional parameters look like variable names just containing digits, but the rules are slightly different. Single digit positional parameters never require (but do allow) braces but multi-digit positional parameters always require braces. The expansion of $11 (or of ${1}1) will result in the expansion of the 1st positional parameter followed by the digit 1. To get the expansion of the 11th positional parameter, you must use ${11}.
The Following User Says Thank You to Don Cragun For This Useful Post:
rbatte1 (07-27-2015)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SCO 5.6 File truncation chssastry Post Here to Contact Site Administrators and Moderators 0 02-06-2008 09:17 PM
STDOUT truncation JerryHone UNIX for Advanced & Expert Users 3 12-03-2006 03:35 AM
PS output truncation in AIX moe2266 AIX 2 11-01-2005 05:51 PM
What is the cause of file truncation? venkatmyname UNIX for Advanced & Expert Users 9 10-04-2005 06:23 PM
truncation when vi a file selur-xinu UNIX for Advanced & Expert Users 0 02-14-2005 05:10 PM



All times are GMT -4. The time now is 12:26 PM.