Execute bash scripts on entering a directory

Heavy Gray

What is the best way to execute a script when entering into a directory?
When I move into a new directory I would like bash to execute the projectSettings.bash script much like RVM does.

Gilles 'SO- stop being evil'

You can make cd a function (and pop and pushd), and make it detect if you enter that particular directory.

cd () { builtin cd "$@" && chpwd; }
pushd () { builtin pushd "$@" && chpwd; }
popd () { builtin popd "$@" && chpwd; }
unset_all_project_settings () {
  # do whatever it takes to undo the effect of projectSettings.bash,
  # e.g. unset variables, remove PATH elements, etc.
}
chpwd () {
  case $PWD in
    /some/directory|/some/other/directory) . ./projectSettings.bash;;
    *) unset_all_project_settings;;
  esac
}

Do not do this in directories that you haven't whitelisted, because it would make it very easy for someone to trick you into running arbitrary code — send you an archive, so you unzip it, change into the directory it created, and you've now run the attacker's code.

I don't recommend this approach, because it means the script will be executed even if you enter that directory for some reason that's unrelated to working on the project. I suggest having a specific function that changes to the project directory and sources the settings script.

myproj () {
  cd /some/directory && . ./projectSettings.bash
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash cd to a directory by just entering its name

From Dev

Bash script callable from home but not scripts directory

From Dev

Should I execute bash scripts from python with `bash` or `./`?

From Dev

Create bash-script to execute multiple bash scripts

From Dev

How to execute ant scripts in a differing directory to a shell script?

From Dev

Bash recursively execute a command on each directory

From Dev

Execute a command from another directory in bash

From Dev

Is it possible to execute Jenkins jobs from Powershell or Bash or Groovy scripts?

From Dev

Errant error output behavior with perl execute bash scripts on a remote machine

From Dev

Where to store bash scripts that all users may execute on Debian?

From Dev

How to execute scripts in a parent-child behavior in bash?

From Dev

running bash script on server to execute multiple MATLAB scripts

From Dev

Persistant "bash: /home/XXX/.rvm/scripts/rvm: No such file or directory" in terminal

From Dev

How do I fix "/usr/bin/python: No such file or directory" when entering an invalid command in bash?

From Dev

Bash - Read Directory Path From TXT, Append Executable, Then Execute

From Dev

Bash: Execute a command in each directory with file my_suites.cfg?

From Dev

Bash - Read Directory Path From TXT, Append Executable, Then Execute

From Dev

sudo su in bash error "Cannot execute help: No such file or directory"

From Dev

-bash: /bin/cd: No such file or directory - automatically execute ls after cd

From Dev

PATH behaves differently in bash and csh when directory is execute only?

From Dev

Bash: Execute a command in each directory with file my_suites.cfg?

From Dev

How do I get ~/.bashrc to execute all scripts in my ~/Shell directory using a loop?

From Dev

Execute scripts AJAX returns

From Dev

Jenkins execute PowerShell scripts

From Dev

Execute prelogin scripts for SSH

From Dev

scripts won't execute

From Dev

Execute prelogin scripts for SSH

From Dev

Why is there no output when entering # in bash

From Dev

Best directory for shared scripts

Related Related

  1. 1

    Bash cd to a directory by just entering its name

  2. 2

    Bash script callable from home but not scripts directory

  3. 3

    Should I execute bash scripts from python with `bash` or `./`?

  4. 4

    Create bash-script to execute multiple bash scripts

  5. 5

    How to execute ant scripts in a differing directory to a shell script?

  6. 6

    Bash recursively execute a command on each directory

  7. 7

    Execute a command from another directory in bash

  8. 8

    Is it possible to execute Jenkins jobs from Powershell or Bash or Groovy scripts?

  9. 9

    Errant error output behavior with perl execute bash scripts on a remote machine

  10. 10

    Where to store bash scripts that all users may execute on Debian?

  11. 11

    How to execute scripts in a parent-child behavior in bash?

  12. 12

    running bash script on server to execute multiple MATLAB scripts

  13. 13

    Persistant "bash: /home/XXX/.rvm/scripts/rvm: No such file or directory" in terminal

  14. 14

    How do I fix "/usr/bin/python: No such file or directory" when entering an invalid command in bash?

  15. 15

    Bash - Read Directory Path From TXT, Append Executable, Then Execute

  16. 16

    Bash: Execute a command in each directory with file my_suites.cfg?

  17. 17

    Bash - Read Directory Path From TXT, Append Executable, Then Execute

  18. 18

    sudo su in bash error "Cannot execute help: No such file or directory"

  19. 19

    -bash: /bin/cd: No such file or directory - automatically execute ls after cd

  20. 20

    PATH behaves differently in bash and csh when directory is execute only?

  21. 21

    Bash: Execute a command in each directory with file my_suites.cfg?

  22. 22

    How do I get ~/.bashrc to execute all scripts in my ~/Shell directory using a loop?

  23. 23

    Execute scripts AJAX returns

  24. 24

    Jenkins execute PowerShell scripts

  25. 25

    Execute prelogin scripts for SSH

  26. 26

    scripts won't execute

  27. 27

    Execute prelogin scripts for SSH

  28. 28

    Why is there no output when entering # in bash

  29. 29

    Best directory for shared scripts

HotTag

Archive