2

My Condition is like below.

SEARCH="find `pwd` -name"

bash> $SEARCH resources.xml | grep $SEARCH_DIR | grep $1 | grep ‘cells’

But am not getting the pwd as referenced in that variable.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • SEARCH="find $(pwd) -name" – Serge Jun 09 '16 at 09:28
  • @Serge that would define the variable with the current value of pwd, not the value at the point of execution – Chris Davies Jun 09 '16 at 09:49
  • Verify the value of $SEARCH by echoing it using echo "$SEARCH". And what is the value of $SEARCH_DIR? – Lambert Jun 09 '16 at 10:30
  • @roaima the question is unclear then – Serge Jun 09 '16 at 12:09
  • Were you actually using , or was it a regular "? – Michael Mrozek Jun 09 '16 at 15:00
  • @serge & @roaima When i use this command SEARCH="find $(pwd) -name", then it is coming like this " find /tmp/ -name " , but my requirement is am using the variable SEARCH dynamically so need to come like this "find pwd -name" etc.

    Also if we use a command inside a variable, when the Variable called eg $SEARCH the command(pwd) is executed already. like "find /tmp -name" .. but requirement it should not execute before that..

    – Rajagopalarao Jun 10 '16 at 06:40
  • I believe I have already addressed this requirement in my answer – Chris Davies Jun 29 '16 at 16:49

2 Answers2

3
alias SEARCH='find . -name'

Or

SEARCH() { find . -name "$1"; }

Usage

SEARCH resources.xml | ...

Note that SEARCH is not a variable. You could define the command as a variable (SEARCH='find . -name') but the alias or function definition is more usual practice.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
2

Let's say that the current directory is /original/directory at the time the SEARCH=… line is evaluated and /later/directory at the time the $SEARCH … line is evaluated.

The command substitution `pwd` is evaluated at the time the variable SEARCH is defined, so SEARCH is set to find /original/directory -name. If the current directory at that time doesn't contain any whitespace or wildcard characters, then $SEARCH resources.xml later calls find in that directory, i.e. find /original/directory -name resources.xml.

If the original current directory contains whitespace or wildcards, this won't work. Stuffing a command into a variable like this doesn't work except in very simple cases. See Why does my shell script choke on whitespace or other special characters?

If you wanted to run find in /later/directory, then you could just run find .. But a variable isn't the right tool here. A variable is for storing a string. A command with parameters is not a string, it's a list of strings. And a shell code snippet can be stored in a string, but $SEARCH does not run the value of SEARCH as a shell code snippet (once again, see Why does my shell script choke on whitespace or other special characters?). To store a shell code snippet for later use, define a function.

SEARCH () {
  find "$PWD" -name "$@"
}
…
SEARCH resources.xml | …

Once again, this runs find in /later/directory. If you wanted to run find in /original/directory, the best way is to store that directory in a variable and use that variable later.

original_directory="$PWD"
SEARCH () {
  find "$original_directory" -name "$@"
}
…
SEARCH resources.xml | …