10

If I want to search a file in the system I use the following command:

sudo find `pwd` -name filename.ext

I want to make an alias for an easier word like search, so I used the command:

alias search "find `pwd` -name "

The problem is that the command translates the pwd part to the actual path i'm in now. When i type simply alias to see the list of aliases I see:

search   find /path/to/my/homedir -name

How can I avoid this?

1
  • 4
    which shell are you using?
    – ilkkachu
    Commented Aug 16, 2016 at 9:18

4 Answers 4

22

Use single quotes to avoid shell expansion at time of definition

alias search='find `pwd` -name '
1
  • 3
    Note that it's POSIX sh syntax, not csh syntax. In POSIX sh, the output of pwd would undergo split+glob which you don't want here, you'd need find "$(pwd)" or better still find "$PWD". In csh, quoting (as in find "`pwd`") helps a little in that it disables globbing but the output of pwd is still split (though this time into the non-empty lines in pwd's output instead of the (space, tab and newline searated) words. Commented Aug 16, 2016 at 10:02
4

Modify the command as:

alias search="find . -name "

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

1
  • 1
    Yes, but it will report file paths as ./foo/file.txt, as opposed to /current/dir/foo/file.txt. Also, that's POSIX sh syntax, not csh syntax like in the OP's question. Commented Aug 16, 2016 at 9:33
4

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'
0

alias is evluated once is called. There is no option to call command when callnig alias. You have to write a bash function, where you can run sub commands or, better, use . (search in current dir) instead of `pwd`.

1
  • alias is evluated once is called. There is no option to call command when callnig alias. But of course there is, if the alias contains an expression that evaluates to a command, as long as the OP uses the right quotes to ensure evaluation happens at the right time: unix.stackexchange.com/a/303675/124753 Commented Aug 16, 2016 at 10:08

Not the answer you're looking for? Browse other questions tagged .