[Cialug] shell escaping with command substitution?

Pixie pix at kepibu.org
Tue Feb 10 00:03:57 CST 2015


On 2015.02.09 21:41, Kenneth Younger wrote:
> Alright, a basics question, because apparently my shell-fu isn't strong
> enough to figure this out:
> 
> I have a command I wrote, testpgm. When ./testpgm is run it outputs this:
> 
> '/Users/kenny/test/test 3.txt' '/Users/kenny/test/test1.txt'
> '/Users/kenny/test/test2.txt'
> 
> When I then run cat $(./testpgm), I get errors. If I copy and paste the
> output instead of doing command substitution, it works fine. What gives?
> This is the error:
> 
> cat: '/Users/kenny/test/test: No such file or directory
> cat: 3.txt': No such file or directory
> cat: '/Users/kenny/test/test1.txt': No such file or directory
> cat: '/Users/kenny/test/test2.txt': No such file or directory
> 
> It almost seems like it's double-escaping the output of testpgm.
> 
> Thanks in advance for any insights.

Command substitution[1] isn't a straight textual replacement that hooks
back into the normal shell parser, it does a limited subset of things
afterwords--namely word splitting[2] followed by glob expansion.  In
other words, no parsies the quotesies.

Relevantly, this[3] popped up on Hacker News earlier today.  (My
takeaway?  Use Perl, it'll be less painful.)

[1]
http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution
[2] http://www.gnu.org/software/bash/manual/bashref.html#Word-Splitting
[3] http://www.dwheeler.com/essays/filenames-in-shell.html



More information about the Cialug mailing list