Register
It is currently Fri Sep 19, 2014 7:53 pm

SHould be a simple escape question, and probably is.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Mar 09, 2010 7:28 pm   

Joined: Sat May 30, 2009 5:56 am
Posts: 15
Here's a highly oversimplified example of what I'm trying to do, but exactly illustrates the problem I'm having and should be enough to provide a workable solution.

Here's a simple script that should ls a particular folder, and put the output into a text file:
Code:
#!/bin/bash
path1=/home/name/Black\ \&\ Red
list=/home/name/Black\ \&\ Red.lst

ls $path1 | sort >> $list
exit


As you can see, I have the & character and some spaces in the filename that are being escaped with the backslash.

I've tried the plain escape as above, and I've tried wrapping the path-/file names in both single and double quotes (not at the same time, obviously) both with and without the escape character, but Bash always gives the same error(s) - something along the lines of:
Quote:
file/folder 'Black' not found
file/folder '&' not found
file/folder 'Red' not found
Also, $list ends up just being titled 'Black', so the error is present in that line as well.

One thing I have noticed is that if I get rid of the spaces in the filenames and just escape the '&' symbol, I don't get the error. I, however, like having spaces in my filenames.

How can I properly escape and/or wrap those filenames so they're correctly interpreted by the shell?


Top
 Profile  
 PostPosted: Wed Mar 10, 2010 6:03 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
quote everything;

Code:
#!/bin/bash
path1='/home/name/Black & Red'
list='/home/name/Black & Red.lst'

ls "$path1" | sort >> "$list"
exit


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP