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.
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.
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.
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 | …
SEARCH="find $(pwd) -name"
– Serge Jun 09 '16 at 09:28pwd
, not the value at the point of execution – Chris Davies Jun 09 '16 at 09:49$SEARCH
by echoing it usingecho "$SEARCH"
. And what is the value of$SEARCH_DIR
? – Lambert Jun 09 '16 at 10:30”
, or was it a regular"
? – Michael Mrozek Jun 09 '16 at 15:00pwd
-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