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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-06-2010, 07:58 PM
|
#1
|
|
Member
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD, PC-BSD
Posts: 31
Rep:
|
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.
|
|
|
|
06-07-2010, 08:15 AM
|
#2
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Code:
variable="$(foo \"$file\")"
Not sure if it will work, but I would try it.
|
|
|
|
06-07-2010, 09:00 AM
|
#3
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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.
|
|
|
|
06-07-2010, 09:08 AM
|
#4
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
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.
|
|
|
|
06-09-2010, 03:30 PM
|
#5
|
|
Member
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD, PC-BSD
Posts: 31
Original Poster
Rep:
|
Quote:
Originally Posted by MTK358
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.
|
|
|
|
06-09-2010, 04:00 PM
|
#6
|
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,006
Rep: 
|
Use catkin's answer, it's the right one (a slight nitpick is that the $foo in the echo should be quoted).
|
|
|
|
06-09-2010, 10:21 PM
|
#7
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by tuxdev
(a slight nitpick is that the $foo in the echo should be quoted).
|
Nitpicks are welcome 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:52 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|