Cannot expand variable in backquote

Asked 1 years ago, Updated 1 years ago, 309 views

A shell script that moves a file containing the string "ice" into the "ice" folder.
There are two ice creams in the cord.I'd like to do this only in one place, but I don't know how to write it.
I would appreciate it if you could let me know.

search_name='ice'
mkdir$search_name
names=`find.-type f-name' *ice*'-maxdepth1 | sed's /.\//'`
echo$names | while read file_name
do
    echo$file_name
    mv$file_name$search_name/
done

Code that didn't work

names=`find.-type f-name*$search_name*-maxdepth1 | sed's /.\//'`
names=`find.-type f-name*${search_name}*-maxdepth1 | sed's /.\//'`
names=`find.-type f-name'*${search_name}*'-maxdepth1 | sed's /.\//'`

bash shell

2022-12-29 21:30

1 Answers

Read man in bash.

Deploy Pathname

In the first and second cases of the question, there is no quote, so pathname deployment occurs.

If the -f option is not specified, after a word split, bash checks to see if each word contains *, ?, [. If any of these characters are found, the word is considered a pattern and replaced with an alphabetically sorted list of filenames that match the pattern.

For example, * is expanded by bash as shown in the following example.

$mkdir tmp
$ cd tmp
$ touch hoge
$ echo*
hoge
$ cd..
$ rm-rtmp

Because this pathname deployment is done by bash before find is invoked, the -name option in find cannot be specified correctly. A quote is required to pass to find correctly without expanding the *.

$ls-1
a1
a2
$ find.-name '*'
.
./a1
./a2
$ find.-name*
find:paths must precede expression:`a2'
find —Possible unquoted pattern after predictor `-name'?
$ # If there is no single quote, the pathname will be expanded in bash, so it is the same as below.
$ find. - name a1 a2
find:paths must precede expression:`a2'
find —Possible unquoted pattern after predictor `-name'?
$ 

For Quotes , use

Quoting removes the special meaning of a particular character or word to the shell. Quotes can be used to disable the special handling of special characters, to prevent reserved words from being identified as reserved words, and to prevent parameter expansion.

and so on.This means that * may not have any special meaning when quoted.

If you read it continuously,

There are three ways to quote: escape character, single quote, and double quote.

Yes, and if you keep reading it,

When you enclose characters in a single quote, each character inside the quote retains its value as a character.

If you enclose characters in a double quote, all characters inside the quote retain their value as characters, except $,,\. $ and do not lose special meaning inside double quotes.

You can see that * is a single quote ('), but $ is not a special character, so


2022-12-31 00:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.