Focus chính bài viết: hiểu rõ chuyện gì xảy ra khi bạn run script vs source script trong bash. Trước tiên hiểu cách program submitted khi call script different ways.
NOTE: Tạo script với extension không quan trọng. Script chạy fine even without extensions.
Mọi script bắt đầu bằng line gọi shebang(#!). Hash symbol trong bash interpreted as comments nhưng shebang có special meaning. Nó tell bash submit program trong interpreter bạn mention trong shebang.
Sample program chỉ định bash as interpreter:
$ cat >> Hello_World.sh
#!/usr/bin/env bash
echo "Hello world"
$ chmod +x Hello_world.sh
Chạy script 2 ways:
- Relative path: cd đến directory script, chạy ./Hello_world.sh.
- Absolute path: từ anywhere trong filesystem type full path to script.
$ ./Hello_world.sh
$ pwd
$ /home/karthick/Hello_world
Xem gì xảy ra khi submit program không shebang. Không có shebang, program submitted đến current shell bạn đang chạy, trong case này là Bash (/bin/bash).
Demo: tạo python script không shebang, call program, bash không biết submit đến python interpreter, chạy trong current shell.
$ cat > run-py.py
echo $SHELL
print("Hello world")
$ chmod +x run-py.py
$ ./run-py.py
Trường hợp này, call program mention interpreter hoặc thêm shebang line (luôn recommended).
# which python3
$(which python3) /home/karthick/run_py.py
Biết cách call script, bước tiếp theo: hiểu gì xảy ra khi call script. Khi invoke script như examples, nó tạo child process (forking) và script submitted to child process. Chạy sample script chạy command sau show script submitted to child process.
$ ps -ef --forest | grep -i bash
Có thể multiple child processes phần của script, tùy code. Lưu ý: environmental variables tạo bởi subscript dropped khi finished. Child process access variables tạo bởi parent process bằng exporting. Nhưng parent process KHÔNG access variables tạo bởi child process.
Xem thêm articles hiểu variables và export variables:
- Understanding and Writing 'Linux Variables' in Shell Scripting
- Learn Difference Between $$ and $BASHPID in Bash
Sourcing the Script
"Source" là shell built-in command đọc file passed as argument và chạy code trong current shell environment. Use case phổ biến: modify config trong .bashrc hoặc .bash_profile và reload changes dùng source command.
$ type -a source
2 syntactic ways chạy source command. Chọn bất kỳ, personal choice.
$ source FILE_NAME [ARGUMENTS]
$ . FILE_NAME [ARGUMENTS]
Demo cách source actually works. Tạo 2 shell scripts. Script 1 (Module.sh) hold variables và functions. Script 2 (Main.sh) print variable và call function.
File Module.sh.
#!/usr/bin/env bash
VAR1=$(echo "Welcome to $1")
function f1(){
echo "Function f1 is called"
}
File Main.sh.
#!/usr/bin/env bash
echo $VAR1
f1
Set execution permission và call main script "main.sh". Script sẽ try find function f1 và variable VAR1 trong current shell environment và fail với command not found.
$ bash main.sh
Chạy source command bên trong script load variable và functions vào current shell environment, accessible bởi "main.sh".
File Module.sh.
#!/usr/bin/env bash
VAR1=$(echo "Welcome to $1")
function f1(){
echo "Function f1 is called"
}
File Main.sh.
#!/usr/bin/env bash
source module.sh Tecmint
echo $VAR1
f1
Chạy script lại và xem.
$ bash main.sh
Source rất useful trong bash follow modular programming approach tạo shell scripts. Break code thành smaller modules dùng trong nhiều programs. Follow DRY (Don't Repeat Yourself) principle.
Đó là tất cả bài viết. Thảo luận ngắn gọn difference giữa sourcing và forking trong bash. Đọc bài viết và share feedback.