Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

ksh: use find command with quotings

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
Honored Contributor 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
Acclaimed Contributor Acclaimed Contributor

Re: ksh: use find command with quoting

[ Edited ]

>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*

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

Acclaimed Contributor 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.