JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Including a script file in another Bash script

Author:JIYIK Last Updated:2025/03/22 Views:

This article discusses different ways to include a Bash script file into another script file.


Including files in Bash scripts

Including or reusing scripts in Bash is very simple. The source keyword is similar to that in C/C++ #include.

To reuse a script, use the source keyword with the name/full path to the file, depending on whether you are importing the script from the same directory or from another directory.

Typically, the syntax is as follows:

source <filename>

Assume we have a script named sorting.sh with the following content.

TEST_VAR="Hello"

The syntax is as follows.

Bash Script:

#!/bin/bash
source sorting.sh
echo ${TEST_VAR}

The alternative .operator is a shorthand for the source keyword and works similarly.

Bash Writer:

#!/bin/bash
sorting.sh
echo ${TEST_VAR}

Both of the above examples will return the following output.

Output:

Hello

请注意The operator is POSIX compliant, while the source keyword is not. This means that .the operator will work in all POSIX shell environments, while source may not work, but in the context of Bash, it is perfectly safe to use the source keyword.

Another point to note is sourcethat works includedifferently than in C/C++ in that it will execute the complete source script within the target script rather than calling and executing separate functions individually.

The safest way to use a script within another script is to use the following script which will work for all levels of directories and will not cause errors due to directories being different or at different levels.

Bash Writer:

$(dirname "$0")/sorting.sh

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

eval command in bash script

Publish Date:2025/03/21 Views:88 Category:OPERATING SYSTEM

This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values ​​(for example)

Exit Bash Script

Publish Date:2025/03/21 Views:99 Category:OPERATING SYSTEM

This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting. What is Bash Scripting A computer script/program tel

Echo Tab Character in Bash Script

Publish Date:2025/03/21 Views:66 Category:OPERATING SYSTEM

This article explains how to echo one or more tab characters when using Bash scripts. Echo Tab Character in Bash Script echo The command is simple: it prints whatever is passed to the terminal. Normally, if you save a variable as: example=

Bash 脚本中的 eval 命令

Publish Date:2023/06/11 Views:388 Category:操作系统

本文是关于在 Bash 脚本中使用字符串作为命令的。 为此,使用了 eval 命令。Bash 脚本中的 Eval 命令 在某些 Bash 脚本中,您必须使用变量或输入值(例如)创建一个字符串,并在最后将其作为命

退出 Bash 脚本

Publish Date:2023/06/11 Views:268 Category:操作系统

本文简要介绍 Bash 脚本,并讨论在出现错误时退出 Bash 脚本。 它进一步讨论了 Bash 脚本的局限性和好处。什么是 Bash 脚本 计算机脚本/程序告诉计算机做什么和说什么。

Bash 脚本中的 Echo Tab 字符

Publish Date:2023/06/11 Views:252 Category:操作系统

本文演示了如何回显特殊字符(例如制表符),否则这些字符会在 Bash 脚本中转换为单个空格字符。 这扩展到 Bash 以外的 shell(例如 zsh)。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial