Nested double quotes won't work in my bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested double quotes won't work in my bash script?
# 1  
Old 04-18-2013
Nested double quotes won't work in my bash script?

In a bash script I have:

Code:
LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name \"files*pkg\""
ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null)

But I get nothing returned. It's just all blank. If I run the find command in a terminal, I get dozens of hits.

I figure it's the way how I'm escaping the double quotes, but after several google articles, I'm pretty sure it's allowed in bash. I even tried single quotes, 'files*pkg', but that didn't work either.

Any help?

Thanks!

Last edited by Scrutinizer; 04-18-2013 at 03:57 PM.. Reason: code tags
# 2  
Old 04-18-2013
What happens when you use set -x option?
# 3  
Old 04-18-2013
Just don't use the escaped double quotes at all. Try without them and it will fly.
# 4  
Old 04-18-2013
Quote:
Originally Posted by superbbrr
But I get nothing returned. It's just all blank.
You cannot quote quotes and expect the shell to unquote them for you. When you execute $LSCMD, it takes those quotes literally. Leave them out...

You might also be in for another surprise -- $LSCMD will still substitute wildcards -- everywhere, because quotes have no meaning inside it. So if you have a file named 'files2pkg' in the current directory, it can shove that in instead of files*pkg. You can turn this off with set -f, so try this:

Code:
LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name files*pkg"

set -f # Prevent * from expanding when $LSCMD splits.
ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null)
set +f # Turn * expansion back on

Another thing you could do is use a function instead of storing things in a string. You wouldn't need to do any special quoting of anything, it'd just be code as usual.

Code:
lscmd() {
        find /project/media -mindepth 2 -maxdepth 2 -name "files*pkg"
}

ALL_PACKAGES=$( lscmd | sort 2>/dev/null )


Last edited by Corona688; 04-18-2013 at 04:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script won't run because hardware won't produce display

Can anyone offer any advice on how to modify the script below to work on a new system we have, that has no graphics capability? We admin the system through a serial RAS device. I've tried running the below script through the RAS and through an ssh -X session. It failed with something like "GTK... (3 Replies)
Discussion started by: yelirt5
3 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

4. Shell Programming and Scripting

Use double quotes as part of the string in a Bash array

So I need to create an array that has " in the string of the text: string = ( "value 1" "value2" where the actual string is "value1" with the quotations included would this work? string = ( \"value1\" \"value\") and if the strings contain spaces as well: string = ("\"this... (4 Replies)
Discussion started by: os2mac
4 Replies

5. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

6. Shell Programming and Scripting

How to count number of double quotes in bash

Need a little help. I have just a simple string with a lot double quotes in it. I need to be able to parse through this string, and know how many double quotes I have, and where I am, so I can key off every 9th double quote. For example (coding is not complete): #!/bin/bash count=0... (3 Replies)
Discussion started by: Akilleez
3 Replies

7. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. UNIX for Dummies Questions & Answers

A very simple script, but alias won't work

I am new to unix and therefore I did a lot of reading before posting. So please, if this has been answered before, forgive me for re-posting and point me to the right place for the answer. I have spent many hours searching the net and read over 50 posts in this forum and even tried a few thing but... (20 Replies)
Discussion started by: sssccc
20 Replies

9. Shell Programming and Scripting

bash script won't execute (Mac)

I can't get any bash scripts to run in Terminal (Mac - Snow Leopard). I have the following super-simple script, and I can't get it to execute despite having the correct permissions (I think). #!/bin/bash echo "WORK... PLEASE?!" I named the file 'testScript.sh', and I added execution... (6 Replies)
Discussion started by: compulsiveguile
6 Replies

10. Shell Programming and Scripting

escaping double-quotes inside the script?

I'm having a strange problem with escaping double-quotes. I have a script that looks like this: #!/bin/bash for HOST in `cat $INFILE | grep -v ^#` do for VFILER in `some_command` do echo " " echo -e '\E The problem with ssh command... (3 Replies)
Discussion started by: GKnight
3 Replies
Login or Register to Ask a Question