Use of $ in shell script
I have the following script that counts the number of A/a entered by user. But I do not completely understand the line 4 and 6. I will be thankful if some one explains what is the special use of $ here.
1. #!/bin/bash - 2. echo "Enter A or a" 3. read text 4. A="${text//[^a|A]}" 5. echo "$A" 6. echo "${#A}" O/P ------------ $ ./test_move Enter A or a AAALL AAA 3 |
http://www.gnu.org/software/bash/man...Expansion.html
pattern substitution (4) and string length (6) |
Pattern search and replace:
There are 4 type of pattern search and replaces,
In your case its type 4: let's say you input text="A a B X a AAaa" here PARAMATER is "A a B X a AAaa" and the PATTERN that is provided is [^a|A] i.e, not a or not A "${text//[^a|A]}" means replace all which are not either a or A in text with nullstring. so the space and the letters other than a or A will be removed. the result will be AaaAAaa "${#A}" means length of the string in variable A. result will be 7 I hope i have made it clear. :) more: Parameter expansion |
Thank you
That was a good explanation from mddesai, also thank for the links posted by mddesai and Ygrex.
The links help me get more information that I was looking for, appreciate it. |
All times are GMT -5. The time now is 02:57 AM. |