Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to customize the functionality of cd command as per my needs.

I define following function - function cd () { cd "$@" && pushd "$@"; }

The intent of this function is to automatically push the directory onto the stack so that it saves me the effort to manually type pushd . everytime.

However the above function is an infinitely recursive function as the call to cd is interpreted to be the function itself and not the cd built-in.

How do I reference the cd built-in in this function?

I know that aliases can be escaped using \. What is the way to escape functions or reference built-ins in a more explicit way?

Note: I do not want to rename my function to anything else.

share|improve this question
1  
Why not just alias cd=pushd? What do you expect to happen when you cd to something that isn't an absolute path (eg, cd ../)? –  Patrick Nov 27 '13 at 12:36

2 Answers 2

up vote 1 down vote accepted

The command builtin forces a command name to be interpreted as a built-in or external command (skipping alias and function lookup). It is available in all POSIX shells including bash.

cd () { command cd "$@" && pushd "$@"; }

(Note that this example is a bad one: it doesn't work with relative paths, and you might as well just type pushd in the first place.)

In bash and zsh (but not ksh), you can use builtin to force a command name to be interpreted as a builtin, excluding aliases, functions and external commands.

share|improve this answer

Bash has a (builtin) command builtin, which does exactly what you need. Replacing cd with builtin cd in your function will fix the recursion.

share|improve this answer

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.