Trying to use `pwd` inside an alias giving unexpected results

Use single quotes to avoid shell expansion at time of definition

alias search='find `pwd` -name '

Modify the command as:

alias search="find . -name "

So it will always search in current directory only i.e. Present Working Directory


command substitution is expanded inside double quotes in (t)csh (that you seem to be using) like in Bourne-like shells.

So in:

alias search "find `pwd` -name "

You're actually doing something like:

alias search 'find /some/dir -name '

Where /some/dir was the current directory at the time that alias command was run.

Here, you want:

alias search 'find $cwd:q -name'

$cwd is automatically set by tcsh (so is $PWD in modern versions, like in POSIX shells), so you can use it in place of the less efficient and less reliable `pwd`.

We use single (strong) quotes so that that $cwd is not expanded within.

$cwd:q is to pass the value of the variable as one argument as opposed to let it undergo splitting.

Also note that you don't need the space after -name above.

If you wanted to use pwd (for instance so that you get the canonical (symlink-free) path to the current working directory as in some pwd implementations like the GNU one when POSIXLY_CORRECT is not in the environment), you'd use:

alias search 'find "`pwd`" -name'

Though that wouldn't work if the path of the current directory contains newline characters.

Note that you can't use sudo search as aliases are only expanded in command position in (t)csh. In POSIX shells you could do:

alias sudo='sudo '

To tell the shell that the word following sudo should also undergo alias expansion, but that trick doesn't work in (t)csh.

The POSIX sh (or bash/zsh/ksh...) equivalent would be:

alias search='find "$PWD" -name'