linux - 使用 Bash 从简单的 linux 脚本编辑变量
问题描述
我在那里有那个简单的脚本,我想做的是在我从 bash 运行 ./prA.sh 时编辑那个 A 变量以输出 5。
但我想知道如何通过命令行或 bash 来做到这一点。我不想使用 Vi 或任何其他编辑器编辑脚本。
解决方案
如果我理解你的问题是正确的,你有一些选择来解决你的问题,这里我将展示两个。
$ 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
提示:不要粘贴代码的图像,始终使用代码块直接在站点中插入代码。
推荐阅读
- openapi - 将 mTLS Cloudflare 添加到 OpenAPI 3.0.0 yaml 文件
- r - 创建每月虚拟变量
- javascript - Dompdf 无法正确排列 html 元素
- javascript - 过滤器中的输入范围在移动设备上不起作用
- r - 如何找到 LISA.R 以在 R 中绘制 LISA 地图?
- version-control - 在 Perforce 中使用类型映射编辑更新所有文件
- javascript - Firebase & 网站关系信息争夺
- html - 在背景图像 css 上覆盖身体
- node.js - 您如何在 TypeORM 实体中定义多态关系?
- python - 为什么 *import ...* 和 *from x import y* 习语在这里表现如此不同?