Seek ur help on my script !!
Dear Gurus,
I have a script.. $ num=10 $ echo $num 10 $ echo `let num=num+1` $ let num=num+1 $ echo $num 11 Where lies the problem in case of echo `let num=num+1` why does it output blank ?? Suggestions plz gurus !! |
Deleted, because I didn't realize what I said came off as rude.....
|
The part in bold
Code:
$ echo `let num=num+1` Code:
let num=num+1 Also notice, that command substitution invokes a subshell. Therefore, after the operation is finished num will still have its old value as if nothing happened. Subshells do not export their values to the parent shell. |
You could try (if bash)
Code:
echo $((num=num+1)) |
The only problem I see with the idea, is using the command 'let'
Not real sure what the reason behind it is. You can use just regular shell arithmetic; Code:
num=$(( num + 1 )) |
The idea of using echo is to simultaneously output the result
|
@ crts,
I agree with you. This could be the exact reason. But the same command when run at the command line, (in Bash) let var=var+1 it works fine. Maybe its running in the same process. I mean no additional sub-shell must be running for it, Right?? Please suggest if i am correct. |
Quote:
http://mywiki.wooledge.org/BashFAQ/024 http://mywiki.wooledge.org/SubShell http://mywiki.wooledge.org/BashFAQ/082 |
@ crts,
but what about this command when run in the bash : echo "The world is a `echo "nice place to stay"`" The world is a nice place to stay In this case why the `echo "nice place to stay"` is working fine ? Why does the sub-shell in this case returning correctly to the parent process ? |
Another solution to increment and display result is:
Code:
echo $((++var)) |
Quote:
Code:
`echo "nice place to stay"` Code:
nice place to stay Code:
let num=num+1 BTW, you really should not use backticks `...` for command substitution. Use $(...) instead. Read this link again, why $(...) is preferred over `...` (backticks): http://mywiki.wooledge.org/BashFAQ/082 |
All times are GMT -5. The time now is 01:18 PM. |