Bash is slow to start because of this line in .bashrc. What could cause this?
Because the command substitution is inside double-quotes, it is evaluated at the time that the command is defined. This causes find
to look through your hard disk contents while .bashrc
is running.
You, by contrast, appear to want it evaluated at the time of use. In that case, use single quotes:
alias prog='/path/to/script.sh $(find "$(pwd)" -name prog)'
Note that this alias will fail if any of the files found have whitespace in their names. To avoid that:
alias prog='find . -name prog -exec /path/to/script.sh {} +'
This latter form will work reliably for any kind of file name.
Related videos on Youtube
Peter Mortensen
Updated on September 18, 2022Comments
-
Peter Mortensen almost 2 years
My
.bashrc
file contains a line to this effect:alias prog="/path/to/script.sh $(find $(pwd) -name prog)"
When I comment out this line, Bash starts almost instantly when I open a new terminal. With this line, there is a 4-5 second delay before my cursor shows up.
Removing the nested commands
$(pwd)
, etc. speeds it up again as well. Why is this happening? Can I still use nested commands somehow?-
Admin about 7 yearsThe problem is the
find
command, which is execute at boot time and can take a long time to execute, depending on the directory structure. If you use single- instead of double-quotes,find
will be executed at run time, when thealias
is invoked. It depends on which working directory you want to use, that current at declaration time, or at run time. By the way$(pwd)
is a rather inefficient way to express.
or$PWD
. -
Admin about 7 yearsI know that's not what you are asking, but this looks a lot like a XY problem. Why do you need to locate the script using
find
? Once you installed it, you know where it is. Even if you update it frequently and change its name, you can maintain a symbolic link to it so it's always available under a fixed name. -
Admin about 7 years@AFH Please post answers as answers, not comments.
-
Admin about 7 years@DavidRicherby - I wanted to make sure that this was indeed the answer before posting. I was puzzled that the script would work on files found within an arbitrary directory.
-
-
rleir about 7 years'time of use'. And your second line avoids spawning a subshell.
-
el.pescado - нет войне about 7 yearsOn the downside, now every time you type
prog
,find
would need to be run, whereas before, it would be run only once (on shell startup). -
reinierpost about 7 yearsThen again, owing to file caching, the second time you run the same
find
it's usually much faster. -
David Richerby about 7 years@el.pescado On the upside, it's hard to imagine that the intended behaviour is to run the command on the files that existed at the time the user logged in, rather than the files that exist when they executed the command to process them.
-
Alex Vong about 7 years@DavidRicherby Maybe we can cache the path of
prog
and re-runfind
if the old path no longer exists?