Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm at a Hackathon right now so if someone would answer this soon, you'd be doing me a huge favor.

I've written a function called ls() in my .bashrc that looks like this:

ls() {

  if(some condition);
    do something
  else
    ls; #(CALL TO ORIGINAL ls SYSTEM CALL)
  fi

}

But here when it goes to the else part, it just enters an infinite loop

How can I call the original ls system call in else Or, how can I make ls behave differently in one folder and normally in the others.

share|improve this question
    
did you try to call ls with full path in your else condition? /usr/bin/ls –  quidage Jun 16 '13 at 18:15

2 Answers 2

up vote 5 down vote accepted

You are looking for command:

command [-pVv] command [arg ...]

Run command with args suppressing the normal shell function lookup. Only builtin commands or commands found in the PATH are executed. If the -p option is given, the search for command is performed using a default value for PATH that is guaranteed to find all of the standard utilities. If either the -V or -v option is supplied, a description of command is printed. The -v option causes a single word indicating the command or file name used to invoke command to be displayed; the -V option pro‐ duces a more verbose description. If the -V or -v option is supplied, the exit status is 0 if command was found, and 1 if not. If neither option is supplied and an error occurred or command cannot be found, the exit status is 127. Otherwise, the exit status of the command builtin is the exit status of command.

Example:

$ pwd() {
> echo 'command test'
> command pwd
> }
$ pwd
command test
/etc

There is also builtin, which is more restrictive:

builtin shell-builtin [arguments]

Execute the specified shell builtin, passing it arguments, and return its exit status. This is useful when defining a function whose name is the same as a shell builtin, retaining the functionality of the builtin within the function. The cd builtin is commonly redefined this way. The return status is false if shell-builtin is not a shell builtin command.

share|improve this answer
    
Thank you...... –  Programming Noob Jun 17 '13 at 16:15

You may also be interested in which, which gives you the path to the program in your $PATH with that name.

$ which ping
/bin/ping

(Though I'd recommend Nykakin's answer, as it solves your problem. This is just another tool for anyone interested)

share|improve this answer
    
Thank you very much. This was helpful. –  Programming Noob Jun 17 '13 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.