SHould be a simple escape question, and probably is.

The Hello world script, Commenting / Documentation, echo command, vi, emacs, other text editor comments, shell command or program?.

SHould be a simple escape question, and probably is.

Postby uncertain » Tue Mar 09, 2010 7:28 pm

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: Select all
#!/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:
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?
uncertain
 
Posts: 15
Joined: Sat May 30, 2009 5:56 am

Re: SHould be a simple escape question, and probably is.

Postby DarthWavy » Wed Mar 10, 2010 6:03 am

quote everything;

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

ls "$path1" | sort >> "$list"
exit
User avatar
DarthWavy
 
Posts: 151
Joined: Thu Oct 11, 2007 7:12 am
Location: London - UK


Return to General suggestions and the VERY basics.

Who is online

Users browsing this forum: No registered users and 1 guest