Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Consider:

$ getfacl somefile.dat     # The output is formatted and contains several new lines..

# file: somefile.dat
# owner: user1
# group: group1
user::rw-
group::r--              #effective:r--
mask:r--
other:r--
$
$ ACL_PERMISSIONS=$(getfacl somefile.dat);
$ echo $ACL_PERMISSIONS;
# file: somefile.dat # owner: user1 # group: group1 user::rw- group::r-- #effective:r-- mask:r-- other:r--

So how do I preserve these new lines during variable assignment, so that when I echo $ACL_PERMISSIONS I get the same output as $ getfacl somefile.dat ..?

share|improve this question
    
Kindly explain the reason for the -1..? –  Kent Pawar Apr 17 '13 at 8:42
1  
I'm not the downvoter, but my guess would be that it is because this is an issue merely involving missing quotes (which I don't think warrants a downvote, but I've seen it happen before on questions like this). –  Chris Down Apr 17 '13 at 8:44

1 Answer 1

up vote 3 down vote accepted

You need to quote your expansion, otherwise it will undergo word splitting, which is what you are experiencing.

acl=$(getfacl somefile.dat)
echo "$acl"

Bear in mind that $( strips trailing newlines anyway (it is considered a feature). If that is a problem for you, you need to do something like this to preserve them (in bash4+):

mapfile acl < <(getfacl somefile.dat)
printf %s "${acl[@]}"
share|improve this answer
    
Hey @Chris, Thanks! I wasn't aware of 'word splitting' and using quotes around the variable does solve my problem.. Just curious, how can I preserve trailing newlines (not sure where such a thing might help) using ksh..? –  Kent Pawar Apr 17 '13 at 8:49
    
@KentPawar The first method should also work in ksh. –  Chris Down Apr 17 '13 at 8:55
    
Yup, it does.. Sorry, but didn't quite get your 2nd example.. whats mapfile doing..? –  Kent Pawar Apr 17 '13 at 8:58
1  
@KentPawar From help mapfile: "[r]ead lines from the standard input into an indexed array variable". It essentially reads each line from the output of getfacl somefile.dat into an array called acl. –  Chris Down Apr 17 '13 at 9:00
1  
@Kent - sorry for confusion. "strips newlines" is not a command it is just text. I think in your case you just need to enclose the variable in quotes as Chris suggested. –  suspectus Apr 17 '13 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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