MY code:
$test =$(ps -ef |grep -c "java")
echo "$test"
it's giving me error:
root@test:~# ./restart.sh
./restart.sh: line 1: =4: command not found
There 4 java process running.
Try:
test=$(ps -ef |grep -c "java")
echo "$test"
I think you just need to remove the first $ sign.
To assign output of any shell command to variable in bash, use the following command substitution syntax:
var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)
Edit: "Do not put any spaces before or after the equals sign"
=the correct code is
test =$(ps -ef |grep -c "java")
echo "$test"
doing $test will take the value of the variable test, which is empty at start, thus you code will be equivalent to
=$(ps -ef |grep -c "java")
evaluated at =4 (I suppose you have 4 java-named processes running)
which causes your error