Scripts aanroepen vanuit scripts (Bash)
Versie door Jeroen Strompf (overleg | bijdragen) op 19 jul 2022 om 11:08 (Nieuwe pagina aangemaakt met '[http://stackoverflow.com/questions/8352851/how-to-call-shell-script-from-another-shell-script Stackoverflow]: # Make the other script executable, add the #!/bin/b...')
- Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command
- Call it with the source command (alias is .) like this: source /path/to/script
- Use the bash command to execute it: /bin/bash /path/to/script.
The first and third methods execute the script as another process, so variables and functions in the other script will not be accessible. The second method executes the script in the first script's process, and pulls in variables and functions from the other script so they are usable from the calling script.
In second method, if you are using 'exit' in second script, it will exit the first script as well. Which will not happen in first and third methods.
→ Ik heb prima ervaringen met source
. Wel belangrijk dat de working directory ok is, of dat je absolute padnamen gebruikt.
Casus: Array in een apart bestand
- In bestand
load_site_array.sh
wordt een array geladen - In bestand
test.sh
wordt middelssource load_site_array.sh
die array ingeladen. Deze array is beschikbaar in dit tweede script.