Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

ksh: use find command with quotings

support_billa
Valued Contributor

ksh: use find command with quotings

hello,

 

i want to make "find" variable with different options and store this options in a shell script variable.

 

example ( find a directory named "testdir" ) :

 

mkdir testdir
find . -type d -name "testdir"

 

now variable and it doesn't work:

 

find_opts="-type d -name \"testdir\""
find . ${find_opts} -print     

 

only with eval it works

find . $( eval echo ${find_opts} ) -print

 

Output:


./testdir

- do i need to quote the name of the directory ( -name "testdir"

- does a easier way exist ?

 

regards

4 REPLIES
Patrick Wallek
Honored Contributor

Re: ksh: use find command with quotings

If you are using a complete directory name in your find command, then no you should not need to quote.  Quotes will come into play if you use a wildcard as part of the directory name.

 

For exampe --

 

No need to quote this:

# find /dir -type d -name some_other_dir

 

Quotes needed:

# find /dir -type d -name "some_other*"

Highlighted
Dennis Handly
Acclaimed Contributor

Re: ksh: use find command with quoting

>do I need to quote the name of the directory (-name "testdir")

 

No.

$ find_opts="-type d -name \"testdir\""
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
"testdir"
$ find_opts="-type d -name testdir"
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
testdir

Spaces are a problem:

$ find_opts="-type d -name test dir"
$ printenv $find_opts
the count is 6
printenv
-type
d
-name
test
dir

 

Wildcards may be a problem:

$ find_opts="-type d -name testdir*"
$ mkdir testdir2
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
testdir2

Quoting of var use doesn't help:

$ find_opts='-type d -name testdir*'
$ printenv "$find_opts"
the count is 2
printenv
-type d -name testdir*

 

This doesn't help:

$ find_opts='-type d -name testdir*'
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
testdir2

 

You could use arrays which can maintain quoting:

$ set -A find_opts -- -type d -name "testdir*"

$ printenv "${find_opts[@]}"
the count is 5
printenv
-type
d
-name
testdir*

support_billa
Valued Contributor

Re: ksh: use find command with quoting

hello,

 

printenv  only works with following steps ( ksh )

 

$ find_opts="-type d -name test dir"

$ export find_opts
$ printenv $find_opts

 

and the output is only in one line

 

regards

Dennis Handly
Acclaimed Contributor

Re: ksh: use find command with quoting

>printenv  only works with following steps

 

Sorry, printenv is my own program, not printenv(1).

It also allows me to see what is passed on the command line.