LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-30-2022, 01:23 AM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
find quotation \* substitution weird behaviour


Code:
$ ls
'fil*'   file   file1   test.sh
$ cat test.sh
set -x
arg=fil\\*

[ -f fil\* ] && rm fil\*
find . -name $arg

touch fil\*
find . -name $arg

set +x
$ . test.sh
++ arg='fil\*'
++ '[' -f 'fil*' ']'
++ rm 'fil*'
++ find . -name 'fil\*'
++ touch 'fil*'
++ find . -name 'fil*'
./fil*
./file1
./file
++ set +x
For the first find, bash finds no file to expand fil\* to, and find again finds no file to fit fil\*.
For the second find, bash does expand fil\* to fil*, and find then finds fil*, file1 and file to fit fil*.

Last edited by doru; 07-30-2022 at 01:24 AM. Reason: Added the last '.'.
 
Old 07-30-2022, 02:41 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: Arch
Posts: 4,373
Blog Entries: 5

Rep: Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505
Globs 'fil*', should not be a file name. * is a special character that expands in the shell.

You can force it. Still not good.

Code:
touch 'file*'
This will find everything with file<something> in the name.
Code:
find . -name file*
find . -name "file*"
find . -name 'file*'
This will find everything
Code:
find . name file\*
This will find it.
Code:
find . -name "file\*"

arg="file\*"
find . -name "$arg"
http://mywiki.wooledge.org/BashGuide/SpecialCharacters
 
Old 07-30-2022, 03:15 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,481

Rep: Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091
The \ is the escape character and preserves the literal value of the * character. It does not perform any wildcard expansion.
The command touch fil\* creates the file named fil* and that is why you see 'fil*' in the output of the ls command.

Code:
[ -f fil\* ] && rm fil\*
find . -name $arg
touch fil\*

++ '[' -f 'fil*' ']'
++ rm 'fil*'
++ find . -name 'fil\*'
Since fil* actually exists it is deleted by the rm command and therefore why the find command does not find the file but then you create it again using touch.

Since there is no wildcard expansion rm does not delete file or file1 therefore show up with the second execution of the find command because the \ is not included.

Last edited by michaelk; 07-30-2022 at 03:19 PM.
 
Old 08-02-2022, 04:07 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,094

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Please stick to the rule
"put double-quotes around a $variable in command arguments"!
Code:
find . -name "$arg"
Things become more clear if the shell substitutes it but does not run any further expansions.
Note that find itself runs a glob filename generation on a -name pattern.
 
  


Reply

Tags
find, substitution


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
[SOLVED] weird behaviour of redirection in command substitution in find -exec doru Programming 2 07-30-2022 06:38 AM
[SOLVED] weird find options variable substitution behaviour doru Programming 3 07-29-2022 10:04 PM
can not input double quotation marks(invalid quotation key) wangzhilife Linux - Hardware 2 09-18-2009 06:44 AM
HP Photosmart weird weird weird.... Vlad_M Linux - General 5 02-20-2005 05:41 AM
quotation marks donīt display in X Obi-Wan_Kenobi Slackware 1 08-31-2003 10:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration