Shell learning: Call between scripts

Source: Internet
Author: User

In Java and python, you can use the import method to call scripts or modules. For example:

>>> import math>>> math.sqrt(4)2.0
In Shell, how do I call other shell scripts, or variables and functions in other scripts?

Method 1:../Subscript. Sh

Method 2:Source./Subscript. Sh


Note:

  • There is a space between two points. Please note that.
  • The two scripts are not in the same directory and must use an absolute path.
  • For simplicity, the first method is usually used.


For example:

  • Main. Sh # Main script
  • Subscripts. Sh # subscript, or called script

### Subscripts. Sh script content :####! /Bin/bashstring = "Hello, world! \ N"
### Main. Sh script content ####! /Bin/bash ../subscripts. shecho-e $ {string} exit 0
Output result:

# chmod +x ./main.sh# ./main.shHello,World!#


Note:
  • The script to be called can have no execution permission, and the script to be called must have the executable permission.
  • Chmod + X./Main. Sh # Note that there is something here, otherwise the bash script may not be found.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.