I have this program that I want to tell the user how may time a user inputted word appears in a file. This is what i have written so far but it doesn't work.

#! /bin/bash
echo "Enter the file that is to be searched (~/test/new)"
        read location
echo "Enter the search term"
        read search
$result = (grep -w "$search" $location | wc -w)
echo "The search term" $search "appears "$result" time/s in "$location

What could I do to make it work?

  • 1
    In what ways doesn't your script work? Error messages would certainly hint at what is wrong. – dhag Dec 14 '15 at 18:00
  • Your command substitution / assignment is messed up in at least 3 ways: try result=$( ... ) to start – steeldriver Dec 14 '15 at 18:07

The core command that you want should, if I understand correctly, count each occurrence of the word searched for. Assuming GNU grep:

<"$file" grep -Fwo -- "$word" | wc -l

Option -F switches to non-regexp, verbatim string matching. -w does a word search, as in you question, and -o prints each match alone, on its own line, resulting in the proper count being emitted by wc -l.

Now, about prompting, you could take advantage of the fact that bash read has a -p option:

read -p 'Filename: ' file
read -p 'Word: ' word

Assignment takes the form var=value, with no dollar sign or spaces around the equal sign:


Finally, you can take advantage of string interpolation when writing your output:

echo "File '$file' contains this many occurrences of '$word': $count."
  • I edited the file as suggested and i now get the error: File Location: ~/Test/test Word: Hello ./wordFinder.bash: line 10: ~/Test/test: No such file or directory The search term 'Hello' appears '0' time/s in '~/Test/test' – S.Jones Dec 14 '15 at 18:33
  • That's because tilde expansion doesn't happen. You could require the user to type a complete path (not using the ~ character), or have a look at this question for other options: stackoverflow.com/questions/3963716/… – dhag Dec 14 '15 at 18:53

Taking a wild guess here, as I did not try anything.

Tilde expansion is a bit cumbersome in variables, as it is expanded before variables arte expanded.

Does it work OK with file names without a tilde?

Also, you would do good to test some assumptions, like $location being a readable file. You might want to exit with a helpful message. This possibly will also help you debug your script.

tr -sc "\n$input" '[\n*]' <in | grep -xcFe"$input"

...would probably do it pretty efficiently. But it will probably not work especially well if you mean to only match whole words. For that you can do:

tr \[:space:]\[:punct:] '[\n*]' <in | grep -xcFe"$input"

...or perhaps...

tr -c \[:alnum:]_ '[\n*]' <in | grep -xcFe"$input"

...whichever you feel is most appropriate.


I updated what you were attempting to do with some basic debugging, while achieving what I believe you desire:

read -p "Please specify a location/file to be searched...Example=/var/tmp/test.txt     : " location
while [[ ! -f ${location} ]] ; do
  read -p "Unable to located file specified, please try again" location
read -p "Please specify a search term :" search
result=$(grep -w ${search} ${location} | wc -w)
echo "The search term ${search}, appears ${result} times inside of ${location}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.