首页 > 解决方案 > 使用 Bash 从简单的 linux 脚本编辑变量

问题描述

在此处输入图像描述

我在那里有那个简单的脚本,我想做的是在我从 bash 运行 ./prA.sh 时编辑那个 A 变量以输出 5。

但我想知道如何通过命令行或 bash 来做到这一点。我不想使用 Vi 或任何其他编辑器编辑脚本。

标签: linuxbashubuntuscripting

解决方案


如果我理解你的问题是正确的,你有一些选择来解决你的问题,这里我将展示两个。

$ chmod +x test.sh 

$ cat test.sh 
#!/bin/bash
A=${1}      # here you will receive the "aloha", showed in example below
echo $A

$ ./test.sh "aloha"
aloha

$ bash -x test.sh "aloha"   # debugging bash scripts easily
+ A=aloha
+ echo aloha
aloha

# you can see in terminal A still empty, because you just 
# passed "aloha" as argument/parameter to the bash script
$ echo $A

$ export A="aloha" # if you want A to be defined in terminal, export it
$ echo $A # now you have value directly in terminal
aloha

提示:不要粘贴代码的图像,始终使用代码块直接在站点中插入代码。


推荐阅读