ChatGPT解决这个技术问题 Extra ChatGPT

Shell: How to call one shell script from another shell script?

I have two shell scripts, a.sh and b.sh.

How can I call b.sh from within the shell script a.sh?

Can you give some specifics: which OS and which shell(s) or are you just talking about that problem in principle?? Example code would be helpful as well.
This is not really a specific question nor does it demonstrate prior effort to solve the issue.
One issue I was having was that b.sh did not have executable permissions. It might be a good thing to check.
Append ./ before the script name, example, instead: b.sh, use: ./b.sh
If anyone keeps getting No such file or directory error stackoverflow.com/a/2920431/1356559

1
10 revs, 8 users 50%

There are a couple of different ways you can do this:

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; Or call it with the source command (alias is .), like this: source /path/to/script Or use the bash command to execute it, like: /bin/bash /path/to/script

The first and third approaches execute the script as another process, so variables and functions in the other script will not be accessible. The second approach 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 the 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.


remember to chmod a+x /path/to/file or else it's not going to be executable. Only applies to the ./script method.
Remember to change format/encoding of executable files in unix if they are created in DOS and then uploaded to unix environment -> dos2unix