![使用其他 bash 脚本中定义的变量](https://linux22.com/image/194333/%E4%BD%BF%E7%94%A8%E5%85%B6%E4%BB%96%20bash%20%E8%84%9A%E6%9C%AC%E4%B8%AD%E5%AE%9A%E4%B9%89%E7%9A%84%E5%8F%98%E9%87%8F.png)
我有一个 bash 脚本,我在名为 .txt 的文件中定义 UTF-8 希腊符号greek-utfb.sh
。我想运行测试来显示用于打印文件 greek-utfb-scout 中的希腊字母的变量。
--- greek-utfb.sh ----
utfb-greek ()
{
Alph="\u0391" # Alpha
Beta="\u0392" # Beta
Gamm="\u0393" # Gamma
Delt="\u0394" # Delta
Epsi="\u0395" # Epsilon
}
--- greek-utfb-scout.sh ---
greek-utf-scout ()
{
echo -e "Alph: $Alph"
echo -e "Beta: $Beta"
echo -e "Gamm: $Gamm"
echo -e "Delt: $Delt"
echo -e "Epsi: $Epsi"
}
我必须添加什么greek-utfb-scout.sh
才能将希腊语变量翻译为greek-utfb.sh
.
答案1
首先,不要将变量定义放在函数内。相反,更改greek-utfb.sh
为如下所示:
#!/bin/sh
Alph="\u0391" # Alpha
Beta="\u0392" # Beta
Gamm="\u0393" # Gamma
Delt="\u0394" # Delta
Epsi="\u0395" # Epsilon
现在将此文件来源greek-utfb-scout.sh
:
#!/bin/sh
. greek-utfb.sh
greek-utf-scout ()
{
echo -e "Alph: $Alph"
echo -e "Beta: $Beta"
echo -e "Gamm: $Gamm"
echo -e "Delt: $Delt"
echo -e "Epsi: $Epsi"
}
运行后. greek-utfb-scout.sh
,该函数现在按预期工作:
$ . greek-utfb-scout.sh
$ greek-utf-scout
Alph: Α
Beta: Β
Gamm: Γ
Delt: Δ
Epsi: Ε