script recursion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script recursion
# 1  
Old 02-12-2011
Question script recursion

Can someone please explain me why the following script calls it self recursively:

Code:
#!/bin/bash
echo Called
$0

while this not:

Code:
#!/bin/bash
echo Called
$($0)

Thanks
# 2  
Old 02-12-2011
$0 is a built-in shell variable, meaning the 'first' word, that is, command name, and will include the full pathname. Likewise, the first parameter $1 is actually the second word, and $2, the third word, etc...

Putting the $variable inside $($variable) is like typing
Code:
$(/$HOME/bin/scriptname)

at the command line where nothing will happen.
# 3  
Old 02-12-2011
Quote:
Originally Posted by AlphaLexman
Putting the $variable inside $($variable) is like typing
Code:
$(/$HOME/bin/scriptname)

at the command line where nothing will happen.
I can see that, my question is why Smilie
# 4  
Old 02-12-2011
Sorry, I thought maybe you didn't understand $0.

Honestly, I don't know why, it most likely has to do with variable expansion, or pattern matching, just type
Code:
$(/bin/bash)

and nothing happens. Maybe someone else knows WHY?
# 5  
Old 02-12-2011
The second case ($($0)) is using command substitution. The result of this would be to replace the command with the standard output of the command substitution (in this case itself ($0)), and so it would call itself.

OK. It's very late, and I'm having trouble wording that, so search for "Command Substitution" in the man page Smilie

Last edited by Scott; 02-12-2011 at 09:06 PM..
This User Gave Thanks to Scott For This Post:
# 6  
Old 02-12-2011
Thank you scottn! Here's the relevant part..

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
# 7  
Old 02-12-2011
Yes, I should have just quoted the man page instead of babbling on like I invented it Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Recursion in a bash script

So the problem I am having is recursion with in bash. Specifically the program ideally takes either the current or provided directory, lists out everything in that directory, if it finds another directory the script should call itself and go into that directory. I managed to get it to dive into the... (6 Replies)
Discussion started by: mistsong1
6 Replies

2. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

3. Shell Programming and Scripting

Shell script to generate Fibonacci series using recursion

I am facing problem with Shell script to generate Fibonacci series using recursion i.e. recursive function. Here is my script: #!/bin/sh fibo() { no=$1 if ; then return 0 elif ; then return 1 else a1=`expr $no - 1` fibo $a1 ... (10 Replies)
Discussion started by: Tapas Bose
10 Replies

4. Shell Programming and Scripting

recursion script problem

Hi Guys,, I tried to create a recursive function in unix. The following is the code. #/bin/sh function(){ n=$1; if ; then out=1; echo "inside if for 0"; else out = `$n * function "$n-1"`; echo "inside if for $n-1; fi (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

6. Shell Programming and Scripting

How to remove old files without recursion?

Hi folks, I need to write a script which remove files with suffix *.dmp from a specific directory ,older than 30 days and not including recursive subdirectories. I.e: The following command remove recursive all *.dmp files older than 30 days: find $ORACLE_BASE -mtime +30 -type f -name... (5 Replies)
Discussion started by: nir_s
5 Replies

7. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

8. Shell Programming and Scripting

Problem with recursion in subdirectories

Hello ! I need some help with my simple bash script. This script removes all files ( with name given in $1 ) in current dir and subdirectories . The problem is with first loop in the script ( for file in * ; do ) . When I run the sript in my home directory this script display sometimes( ... (5 Replies)
Discussion started by: scotty_123
5 Replies

9. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

10. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies
Login or Register to Ask a Question