LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-06-2010, 07:58 PM   #1
youarefunny
Member
 
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD
Posts: 31

Rep: Reputation: 0
Double Quotes Inside Double Quotes


Hello I have a tiny bit of a problem. In bash I need to use some equivalent of double quotes inside double quotes (or the other way around.)

I need to run the following statement to get the output of foo and store it in a variable while passing foo the $file which probably contains spaces.

Code:
variable=$(foo "$file")
The problem is that foo might return an empty string and if it does I need to catch it which will not happen unless I quote the whole thing.

Code:
variable="$(foo "$file")"
but you can't have double quotes inside double quotes for obvious reasons. (no difference between open and close symbols)

I thought that it might not mess up because it would try to find the result of the whole $() first but it doesn't it just tries to do '$(foo '

Is there any way to escape it just for the $() but not send the quotes escaped to foo?

Thanks for any help.
 
Old 06-07-2010, 08:15 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
variable="$(foo \"$file\")"
Not sure if it will work, but I would try it.
 
Old 06-07-2010, 09:00 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
[mherring@Ath play]$ var='"fred'                                                                                                                              
[mherring@Ath play]$ echo $var                                                                                                                                
"fred                                                                                                                                                         
[mherring@Ath play]$ echo "hello" | sed "/e/$var/"
sed: -e expression #1, char 4: unknown command: `"'                                                                                                           
[mherring@Ath play]$ echo "hello" | sed "s/e/$var/"                                                                                                           
h"fredllo                                                                                                                                                     
[mherring@Ath play]$ echo "hello" | sed "s/e/"fred/"                                                                                                      
> ^C                                                                                                                                                          
[mherring@Ath play]$ echo "hello" | sed "s/e/\"fred/"                                                                                                         
h"fredllo                                                                                                                                                     
[mherring@Ath play]$
So:
SED works as expected when the variable is used (even though the variable is expanded before SED runs the expression??)

The escape is required when trying to use the string containing a quote.
 
Old 06-07-2010, 09:08 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
The syntax works perfectly -- bash is smarter than you thought:
Code:
c@CW8:~$ foo="$( echo "bar" )"
c@CW8:~$ echo $foo
bar
EDIT: and bash is even smarter than that -- the outermost set of double quotes are not necessary
Code:
c@CW8:~$ foo=$( echo bar none )
c@CW8:~$ echo $foo
bar none
because bash does not perform word-splitting on the expression to the right of an assignment "=".

Last edited by catkin; 06-07-2010 at 09:11 AM.
 
Old 06-09-2010, 03:30 PM   #5
youarefunny
Member
 
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
Code:
variable="$(foo \"$file\")"
Not sure if it will work, but I would try it.
I already tried this. Passes "foo/bar.txt" (with quotes around it) as would be expected.
 
Old 06-09-2010, 04:00 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Use catkin's answer, it's the right one (a slight nitpick is that the $foo in the echo should be quoted).
 
Old 06-09-2010, 10:21 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by tuxdev View Post
(a slight nitpick is that the $foo in the echo should be quoted).
Nitpicks are welcome
 
  


Reply

Tags
bash, double, quote, quotes


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
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
shell scripting basics.. how to forward double quotes ?? m_kane Linux - General 3 01-31-2009 11:45 AM
Bash - double quotes don't protect Exclamation marks geoff_f Programming 15 04-15-2007 09:10 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM
US Intl Keybd; Double quotes not showing in some apps Heinz Linux - General 6 01-10-2004 03:48 PM


All times are GMT -5. The time now is 05:37 PM.

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