Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command NOT found when called from inside bash script

Tags:

linux

bash

puppet

I have an application named puppet installed on my Linux box. It is installed at location /usr/test/bin/puppet

This is how .bash_profile looks

export PATH=/usr/test/bin

if I run command puppet apply from console, it works fine but when I call puppet command from inside bash script, it says command not found

#!/bin/bash
puppet apply x.pp

Any ideas on what is wrong ?

like image 550
Gary Avatar asked Dec 05 '25 13:12

Gary


1 Answers

.bash_profile is loaded only if bash is invoked as login shell (bash -l or from a real tty), at least in Debian based distributions bash in a virtual tty (for example when using xterm, gnome-terminal, etc...) is invoked as interactive shell.

Interactive shells loads the configuration from ~/.bashrc.

bash manpage:

~/.bash_profile
   The personal initialization file, executed for login shells
~/.bashrc
   The individual per-interactive-shell startup file

Shellscripts don't load any of these.

You can check which files are opened by any program with strace:

strace ./s.sh 2>&1 | grep -e stat -e open

Possible solutions:

  1. You can export the variable at the beginning of every script:

    #!/bin/bash
    export PATH=$PATH:...
    
  2. Or you can have another file with the desired variables and source it from any script that need those:

    /etc/special_vars.sh:

    export PATH=$PATH:...
    

    script:

    #!/bin/bash
    . /etc/special_vars.sh
    puppet ...
    
  3. Configure the PATH in in ~/.bashrc, ~/.bash_profile and ~/.profile for the user running the script (sub-processes will inherit the environment variables) to have some warranty that the user can run the script from different environments and shells (some bourne compatible shells others than bash do load ~/.profile)

like image 100
Fernando Avatar answered Dec 07 '25 05:12

Fernando



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!