LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Seek ur help on my script !! (https://www.linuxquestions.org/questions/linux-software-2/seek-ur-help-on-my-script-908158/)

techie_san778 10-14-2011 08:03 AM

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 !!

corp769 10-14-2011 08:13 AM

Deleted, because I didn't realize what I said came off as rude.....

crts 10-14-2011 09:04 AM

The part in bold
Code:

$ echo `let num=num+1`
is called command substitution. I.e. that it will return the output of the operation inside the backticks. Since the operation
Code:

let num=num+1
does not return anything as output nothing will be printed on the screen by the 'echo'.
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.

dive 10-14-2011 09:16 AM

You could try (if bash)

Code:

echo $((num=num+1))
That will return the value of num+1 and also update the variable num.

szboardstretcher 10-14-2011 09:27 AM

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 ))

dive 10-14-2011 09:44 AM

The idea of using echo is to simultaneously output the result

techie_san778 10-16-2011 09:56 AM

@ 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.

crts 10-16-2011 02:38 PM

Quote:

Originally Posted by techie_san778 (Post 4499817)
@ 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.

Yes, that is correct. Here are some links for you on the topic of command substitution and subshells:
http://mywiki.wooledge.org/BashFAQ/024
http://mywiki.wooledge.org/SubShell
http://mywiki.wooledge.org/BashFAQ/082

techie_san778 10-17-2011 01:23 AM

@ 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 ?

grail 10-17-2011 03:09 AM

Another solution to increment and display result is:
Code:

echo $((++var))

crts 10-17-2011 06:39 AM

Quote:

Originally Posted by techie_san778 (Post 4500265)
@ 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 ?

As I said earlier, command substitution returns the output of the command(s) run in the subshell.
Code:

`echo "nice place to stay"`
The above command's output is:
Code:

nice place to stay
The command
Code:

let num=num+1
on the other hand, does not have any output. It simply increments the variable 'num' quietly. Therefore, nothing is printed. Since the changed value of 'num' is lost after the subshell exits, the parent shell treats the value of num as if nothing happened.

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.