我只是分配一个变量,但回声$变量显示别的 [英] I just assigned a variable, but echo $variable shows something else

查看:101
本文介绍了我只是分配一个变量,但回声$变量显示别的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一系列案件,其中回声是$ var 可以显示比只是分配不同的价值。发生这种情况无论所分配的值双引号,单引号或不带引号。

我如何获得shell来正确设置我的变量?

星号

预期的输出是 / * Foobar的是自由软件* / ,而是我得到的文件名列表:

  $ VAR =/ * Foobar的是自由软件* /
$回声是$ var
/斌/启动的/ dev /等/家/initrd.img / lib中/ lib64目录/媒体的/ mnt /选择/ proc中...

方括号

预期值为 [A-Z] ,但有时我得到一个字母而不是!

  $ VAR = [A-Z]
$回声是$ var
C

换行(新行)

期望值是一个独立的行列表,而是所有的值都在一行上!

  $猫文件

酒吧
巴兹是$ var = $(CAT文件)
$回声是$ var
富酒吧巴兹

多个空格

我期望的仔细对准表头,而是多个空格要么消失或塌陷成一个!

  $ VAR =称号|计数
$回声是$ var
标题|计数

标签

我认为的两个制表符分隔值,而是我得到两个空间分隔值!

 是$ var $ ='键\\ tvalue
$回声是$ var
键值


解决方案

在上述所有情况下,变量设置正确,但不能正确读取!正确的方法是在引用时的使用双引号的

 回声$ VAR

这给出给出的所有例子的预期值。始终引用变量引用!


为什么呢?

当一个变量的不带引号后,它会:


  1. 分裂其中值被分成多个字上空格(默认):

    / * Foobar的是自由软件* /

    / * Foobar的免费软件 * /


  2. 每个这些话将经历<一个href=\"http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_06\">pathname扩展,其中模式扩展到匹配的文件:

    / *

    / bin中的/ boot 的/ dev 的/ etc /家居 ...


  3. 最后,所有的参数传递给echo,这相隔写出来单个空格的,给予

      / bin中的/ boot / dev的在/ etc /家Foobar的是自由软件的桌面/下载/

    而不是变量的值。


当变量为引用将:


  1. 来取代它的价值。

  2. 有没有第2步。

这就是为什么你应该的总是给所有变量引用,除非你明确要求字的拆分和路径扩展。如 shellcheck 工具是来帮忙的,并警告在上面的所有情况下丢失的报价。

Here are a series of cases where echo $var can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or unquoted.

How do I get the shell to set my variable correctly?

Asterisks

The expected output is /* Foobar is free software */, but instead I get a list of filenames:

$ var="/* Foobar is free software */"
$ echo $var 
/bin /boot /dev /etc /home /initrd.img /lib /lib64 /media /mnt /opt /proc ...

Square brackets

The expected value is [a-z], but sometimes I get a single letter instead!

$ var=[a-z]
$ echo $var
c

Line feeds (newlines)

The expected value is a a list of separate lines, but instead all the values are on one line!

$ cat file
foo
bar
baz

$ var=$(cat file)
$ echo $var
foo bar baz

Multiple spaces

I expected a carefully aligned table header, but instead multiple spaces either disappear or are collapsed into one!

$ var="       title     |    count"
$ echo $var
title | count

Tabs

I expected two tab separated values, but instead I get two space separated values!

$ var=$'key\tvalue'
$ echo $var
key value

解决方案

In all of the cases above, the variable is correctly set, but not correctly read! The right way is to use double quotes when referencing:

echo "$var"

This gives the expected value in all the examples given. Always quote variable references!


Why?

When a variable is unquoted, it will:

  1. Undergo field splitting where the value is split into multiple words on whitespace (by default):

    Before: /* Foobar is free software */

    After: /*, Foobar, is, free, software, */

  2. Each of these words will undergo pathname expansion, where patterns are expanded into matching files:

    Before: /*

    After: /bin, /boot, /dev, /etc, /home, ...

  3. Finally, all the arguments are passed to echo, which writes them out separated by single spaces, giving

    /bin /boot /dev /etc /home Foobar is free software Desktop/ Downloads/
    

    instead of the variable's value.

When the variable is quoted it will:

  1. Be substituted for its value.
  2. There is no step 2.

This is why you should always quote all variable references, unless you specifically require word splitting and pathname expansion. Tools like shellcheck are there to help, and will warn about missing quotes in all the cases above.

这篇关于我只是分配一个变量,但回声$变量显示别的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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