Sara
Sara

Reputation: 2436

bash script: how to run find command and variables

I'm new to it. x can not be recognized from above statement. What is the problem?

x = find . -name "*.java" | wc -l
echo $x

Upvotes: 0

Views: 507

Answers (2)

Nabren
Nabren

Reputation: 582

This should also work:

x=`find . -name "*.java" | wc -l`

Upvotes: 1

Simon
Simon

Reputation: 32873

It should be

x=$(find . -name "*.java" | wc -l)

(Note that there is no space around the = sign)


To answer your question, the problem is

  1. the space after x causes the shell to try to execute the command x which probably does not exist

  2. you want the result of the command to be stored in x, so you need to execute the command (hence the $(...))

Upvotes: 5

Related Questions