Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-18-2010, 03:46 AM   #1
Registered: Jul 2003
Distribution: FreeBSD 8.2 RELEASE
Posts: 607

Rep: Reputation: 32
Basic bash filename escaping question

Can someone please tell me what I have done wrong with my escaping here:

$ php -q get_vid_dimensions.php '/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
Syntax error: Unterminated quoted string
The original filename is:
/home/kovacs/16-Exclusive McDonald's Farmville farm.flv
Old 11-18-2010, 04:33 AM   #2
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
First of all, I would put the filename in double quotes (i.e. ""). Secondly, you're not escaping all the necessary characters. As a general rule, anything that isn't a number, letter (capital/lowercase), underscore, dot, or a dash (hyphen) shouldn't go into a Linux/UNIX filename; that way you can avoid problems like this. You need to escape your spaces as well (and drop the single-quotes around your escaped apostrophe).

This would be more correct, I think:

"/home/kovacs/16-Exclusive\ McDonald\'s\ Farmville\ farm.flv"

Last edited by MrCode; 11-18-2010 at 04:39 AM.
Old 11-18-2010, 04:47 AM   #3
Registered: Jul 2003
Distribution: FreeBSD 8.2 RELEASE
Posts: 607

Original Poster
Rep: Reputation: 32
Unfortunately I have no control over the filename. The reason I put the filename in single quotes is to avoid having to escape spaces. Also some of the other filenames in this batch of media have other special characters in them which would also need escaping if I were to put them in double quotes - it's easier overall to put the filenames in single quotes in this instance.

The problem is with the escape sequence used to escape a single quote inside a filename that is encapsulated by single quotes. From the reading I've been doing, it's not enough to use a single backslash, eg.
'/home/kovacs/16-Exclusive McDonald\'s Farmville farm.flv'
but instead you have to actually make two strings, which bash will concatenate if they are right next to each other:
'/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
The first single quote represents the end of the first part of the filename, then you have your escaped single quote, then another quote to signal the beginning of the second part of the filename. In theory bash should put the whole together into one filename.
Old 11-18-2010, 06:24 AM   #4
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Originally Posted by Kovacs View Post
$ php -q get_vid_dimensions.php '/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
Syntax error: Unterminated quoted string
The quoting is correct in bash so the string is being parsed twice.

Perhaps this would work:
"'/home/kovacs/16-Exclusive McDonald'\\''s Farmville farm.flv'"
Old 11-18-2010, 09:04 AM   #5
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 403Reputation: 403Reputation: 403Reputation: 403Reputation: 403
Indeed, that error is not bash complaining, but the php parser. Bash parses the string, the resultant string then is passed to php:

# echo '/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
/home/kovacs/16-Exclusive McDonald's Farmville farm.flv
That results in an error on the php's side while parsing the resulting string. So, in other words, you need to get bash to produce an output that is valid php syntax for string handling.

# echo "'/home/kovacs/16-Exclusive McDonald\'s Farmville farm.flv'"
'/home/kovacs/16-Exclusive McDonald\'s Farmville farm.flv'
I am no php expert, and in any case what is "valid" depends only on what exactly do your php code expect to receive.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
I question on a bash script and filename spaces... trist007 Programming 13 01-17-2010 07:13 AM
Bash escaping super_bash Linux - General 2 08-06-2009 01:04 PM
filename escaping in bash dash9 Linux - Software 1 06-04-2009 11:36 PM
Another basic bash question Izarf Linux - Newbie 1 05-03-2009 06:56 PM
Basic bash script question kevpatts Linux - Newbie 3 10-12-2005 02:02 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration