BASH中全局变量的值不变 [英] Value of global variable doesn't change in BASH

查看:61
本文介绍了BASH中全局变量的值不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究具有全局变量的BASH脚本.变量的值在函数/子例程中更改.但是,当我尝试在函数外部打印该变量时,该值不会更改.示例代码如下:

I am working on a BASH script which has a global variable. The value of the variable changes in a function/subroutine. But the value doesnt change when I try to print that variable outside the function. The Sample code is as follows:

#!/bin/bash

count=

linux_f()
    {
        let count=100
    }

linux_f

echo $count

echo语句打印空白而不是100为什么全局变量的值不在函数中遍历.

The echo statement prints blank and not 100 Why the value of the global variable doesn't traverse in the function and out.

推荐答案

您的代码对我有用,打印100.这是我使用的代码:

Your code works for me, printing 100. This is the code I used:

count=

linux_f()
    {
        let count=100
    }

linux_f

echo $count

我已经在MSYS上的bash版本2和Fedora Linux的版本3上对此进行了尝试,并且这两种版本均可使用.您确定要执行该脚本吗?尝试在其中放置回声"this is it",以确保显示某些内容.除此之外,我很茫然.

I have tried this with version 2 of bash on MSYS and version 3 on Fedora Linux and it works on both. Are you really sure you are executing that script? Try putting an echo "this is it" in there just to make sure that something gets displayed. Other than that, I'm at a loss.

这篇关于BASH中全局变量的值不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆