Bash is slow to start because of this line in .bashrc. What could cause this?

9,071

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.

Share:
9,071

Related videos on Youtube

Peter Mortensen
Author by

Peter Mortensen

Updated on September 18, 2022

Comments

  • Peter Mortensen
    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
      Admin about 7 years
      The 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 the alias 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
      Admin about 7 years
      I 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
      Admin about 7 years
      @AFH Please post answers as answers, not comments.
    • Admin
      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
    rleir about 7 years
    'time of use'. And your second line avoids spawning a subshell.
  • el.pescado - нет войне
    el.pescado - нет войне about 7 years
    On 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
    reinierpost about 7 years
    Then again, owing to file caching, the second time you run the same find it's usually much faster.
  • David Richerby
    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
    Alex Vong about 7 years
    @DavidRicherby Maybe we can cache the path of prog and re-run find if the old path no longer exists?