LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-14-2011, 08:03 AM   #1
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Rep: Reputation: Disabled
Angry 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 !!

Last edited by techie_san778; 10-14-2011 at 08:05 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-14-2011, 08:13 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Deleted, because I didn't realize what I said came off as rude.....

Last edited by corp769; 10-16-2011 at 08:48 PM.
 
0 members found this post helpful.
Old 10-14-2011, 09:04 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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.
 
2 members found this post helpful.
Old 10-14-2011, 09:16 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
You could try (if bash)

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

Last edited by dive; 10-14-2011 at 09:17 AM.
 
1 members found this post helpful.
Old 10-14-2011, 09:27 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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 ))
 
Old 10-14-2011, 09:44 AM   #6
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
The idea of using echo is to simultaneously output the result
 
Old 10-16-2011, 09:56 AM   #7
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
@ 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.
 
Old 10-16-2011, 02:38 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by techie_san778 View Post
@ 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
 
Old 10-17-2011, 01:23 AM   #9
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
@ 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 ?
 
Old 10-17-2011, 03:09 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Another solution to increment and display result is:
Code:
echo $((++var))
 
Old 10-17-2011, 06:39 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by techie_san778 View Post
@ 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek script to monitor sizes of list of files and send alert if size changes Jon_Roland Linux - Security 3 03-28-2011 04:37 PM
I seek this program ScotHypnotist Linux - Software 2 02-11-2008 12:27 AM
Hello To all and Seek for answers emon-bd Linux - General 3 07-29-2006 04:47 PM
illegal seek bijucnair Programming 7 06-30-2006 08:11 AM
seek errorss CrashedAgain Linux - Hardware 2 11-20-2005 07:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration