从Bash中的变量进行括号扩展 [英] Brace expansion from a variable in Bash

查看:47
本文介绍了从Bash中的变量进行括号扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Bash中扩展一个变量.这是我的示例:

I would like to expand a variable in Bash. Here's my example:

variable="{1,2,3}"
echo $variable

预期输出:

1 2 3

实际输出:

{1,2,3}

推荐答案

由于bash执行命令行扩展的顺序,因此扩展无法正常工作.如果您阅读手册页,则会看到以下顺序:

The expansion doesn't work because of the order in which bash performs command-line expansion. If you read the man page you'll see the order is:

  1. 括号扩展
  2. 波浪扩展
  3. 参数扩展
  4. 命令替换
  5. 算术扩展
  6. 流程替代
  7. 分词
  8. 扩展路径名
  9. 删除行情

参数扩展在大括号扩展之后发生,这意味着您不能像尝试的那样将大括号扩展放入变量中.

Parameter expansion happens after brace expansion, which means you can't put brace expansions inside variables like you're trying.

但请不要害怕,这是答案!要将数字列表存储在变量中,可以使用数组.

But never fear, there's an answer! To store a list of numbers in a variable, you can use an array.

variable=(1 2 3)
echo "${variable[@]}"

这篇关于从Bash中的变量进行括号扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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