I have a script:
setup.sh
read -s -p "Enter your password to use in script: " PASSWORD
echo -e $PASSWORD | sudo -S brew cask install junk
echo "Done Installing junk, running step 1..."
./step1.sh
step1.sh
echo -e $PASSWORD | sudo -S some-other-command
Obviously the $PASSWORD will be empty in step1.sh, and usually you export a variable to use globally in other scripts it invokes:
export PASSWORD
Or you can use it as an script argument:
setup.sh
read -s -p "Enter your password to use in script: " PASSWORD
echo -e $PASSWORD | sudo -S brew cask install junk
echo "Done Installing junk, running step 1..."
./step1.sh $PASSWORD
step1.sh
echo -e $1 | sudo -S some-other-command
I'm assuming the first example using an export isn't a good idea from a security perspective (however, I'm not sure). Is the second usable and secure? Also, if not, is there a better way to do this without relying on sudo caching the password (I have a large list of scripts that take time to execute and the cache will probably timeout and I want the entire script to be unattended)?