0

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

2 Answers 2

5

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 $(...))

Sign up to request clarification or add additional context in comments.

1 Comment

@sweet You probably inserted a space before =.
1

This should also work:

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

1 Comment

For no particular reason (maybe because it's one fewer character?) I prefer this notation. And it's a bit more stack-overflowey - the same backticks we use in markdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.