Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-30-2022, 02:23 AM   #1
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

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

$ ls
'fil*'   file   file1
$ cat
set -x

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

touch fil\*
find . -name $arg

set +x
$ .
++ arg='fil\*'
++ '[' -f 'fil*' ']'
++ rm 'fil*'
++ find . -name 'fil\*'
++ touch 'fil*'
++ find . -name 'fil*'
++ 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 02:24 AM. Reason: Added the last '.'.
Old 07-30-2022, 03:41 PM   #2
LQ Guru
Registered: Oct 2004
Distribution: Arch
Posts: 5,022
Blog Entries: 6

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

You can force it. Still not good.

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

find . -name "$arg"
Old 07-30-2022, 04:15 PM   #3
Registered: Aug 2002
Posts: 24,990

Rep: Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680Reputation: 5680
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.

[ -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 04:19 PM.
Old 08-02-2022, 05:07 AM   #4
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,639

Rep: Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145
Please stick to the rule
"put double-quotes around a $variable in command arguments"!
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.


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

All times are GMT -5. The time now is 04:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration