bash - Why is the assignment in a function ignored?
问题描述
Consider following code:
zzz=2
function f()
{
zzz=4
}
$(f)
echo $zzz
Why is the assignment (zzz=4
) ignored? I thought I knew what $( )
does - it takes everything written to stdout and pastes it verbatim in the place where it's used. But why would that affect assignments to a global variable?
解决方案
你为什么在一个子shell中调用它?
zzz=2
f() { zzz=4; }
$(f) # assigns COPY of zzz, then goes POOF
echo $zzz
f # assigns to zzz
echo $zzz
通过在子外壳中运行它,您正在创建一个子环境,该环境获取分配的变量,然后蒸发。
推荐阅读
- android - 如何在 Jetpack compose 中创建圆角 BottomDrawer(又名 Modal Bottom Sheet)
- node.js - 如何在strapi.io中为用户分配“作者”角色?
- php - Laravel:过时的存档功能,引发错误
- react-native - 无效调用挂钩错误,但挂钩似乎有效
- visual-studio-code - 为什么我的 Visual Studio Code 终端滞后并在延迟后执行程序?
- android - 错误:包 android.support.v7.widget 不存在 import er
- javascript - 如果“centeredSlides:true”不起作用,如何使活动幻灯片居中?
- python - Mypy - 为什么 TypeVar 在没有指定边界的情况下不起作用
- android - Jetpack Navigation如何获取上一个对话框/片段的ID
- java - 无法写入导入的 SQLite 数据库 Android