bash 从linux bash中的函数分配局部变量一个新值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22527325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:57:42  来源:igfitidea点击:

assign local variable from function in linux bash a new value

bashfunctionscopeglobal-variables

提问by Wolfgang Adamec

I have a linux bashscript with a function:

我有一个linux bash带有函数的脚本:

myfunctiona () 
{
  local MYVAR1="one"
  local MYVAR2="two"
  echo $MYVAR1
  # The line beneath is the line in question!
  local MYVAR1=$MYVAR1$MYVAR2       
}

When I want to give the LOCALvariable MYVAR1in the function myfunctionaa new value, do I have to write

当我想给函数中的LOCAL变量一个新值时,我是否必须写MYVAR1myfunctiona

local MYVAR1=$MYVAR1$MYVAR2

or can I also write

或者我也可以写

MYVAR1=$MYVAR1$MYVAR2

With the second line without "local" do I create a global variable with the same name?

对于没有“local”的第二行,我是否创建了一个同名的全局变量?

回答by Geoff Williams

Once you've defined a local variable you can assign it normally, like this:

定义局部变量后,您可以正常分配它,如下所示:

#!/bin/bash

myfunctiona () 
{
  local MYVAR1="one"
  local MYVAR2="two"
  echo $MYVAR1
  # The line beneath is the line in question!
  local MYVAR1=$MYVAR1$MYVAR2    
  MYVAR1="FOO"
  echo $MYVAR1   
}

myfunctiona
echo "global" $MYVAR1

which gives the output:

这给出了输出:

one
FOO
global
  • As you can see attempting to access the variable from global scope returns null
  • 如您所见,尝试从全局范围访问变量返回 null

HTH

HTH

回答by Saucier

The correct way to do it would be:

正确的做法是:

MYVAR1="${MYVAR1}${MYVAR2}"

The braces are usually used when you concatenate variables. Use quotes.

连接变量时通常使用大括号。使用引号

The variable is still local since you reassigned its value within the scope of the function. An example:

该变量仍然是本地的,因为您在函数范围内重新分配了它的值。一个例子:

#!/usr/bin/env bash

_myFunction()
{
    local var_1="one"
    local var_2="two"
    local -g var_3="three" # The -g switch makes a local variable a global variable
    var_4="four" # This will be global since we didn't mark it as a local variable from the start

    var_1="${var_1}${var_2}"

    echo "Inside function var_1=${var_1}"
    echo "Inside function var_2=${var_2}"
    echo "Inside function var_3=${var_3}"
    echo "Inside function var_4=${var_4}"
}

_myFunction

echo "Outside function var_1=${var_1}"
echo "Outside function var_2=${var_2}"
echo "Outside function var_3=${var_3}"
echo "Outside function var_4=${var_4}"

This results in:

这导致:

$ ./script
Inside function var_1=onetwo
Inside function var_2=two
Inside function var_3=three
Inside function var_4=four
Outside function var_1=
Outside function var_2=
Outside function var_3=three
Outside function var_4=four

回答by user3004356

You can give this way, but as Ube said for concatenation you need to give like that -

你可以这样给,但正如宇部所说的串联,你需要这样给——

MYVAR1="$MYVAR1$MYVAR2";   

Even this works for concatenation

即使这也适用于串联