Home > other >  How can I add expansions to a bash argument
How can I add expansions to a bash argument

Time:07-04

I have this simple script to find a file on a folder, with "find" command in the shell works, but in the script doesn't.

#!/bin/bash
echo ""
read -p "-Write file you need- " FILE
read -p "-Write the folder to search- " FOLDER

FILE="'*$FILE*'"

find $FOLDER -name $FILE

If "FILE" is "test" I want to be '*test *' so find can find not only the same name of file, also test.txt and more.

EDIT:

I'm using #!/bin/bash -ex that tell me what's happen in every line of code, if I add

echo $FILE

Its tell me this:

echo $FILE

'* test *'

find "$FOLDER" -name "$FILE"

find /home -name ' ' \ ' ' *test * '\ ' ' '

I don't know why It's adding so many quoting marks and not using the same content of variable like echo

CodePudding user response:

Use quotes where they are needed, like this

#!/bin/bash
echo ""
read -p "-Write file you need- " FILE
read -p "-Write the folder to search- " FOLDER

FILE="*$FILE*"

set -x
find "$FOLDER" -name "$FILE"
  • Related