I write this commands:
#!/bin/bash
echo "Enter values a and b (separate with space)"
read a b
echo $#
And I want to count how many arguments the user has entered. I try to count with $#
, but the output is 0
.
What's the problem? What I am doing wrong?
Here's how I'd probably do it, without thinking too much about it. It's hacky to use the dummy
c
variable, but I find bash array's even more awkward.You may use an array to read complete line and count # of words:
Then print no of words: