Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brace expansion from a variable in Bash

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

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

Expected output:

1 2 3

Actual output:

{1,2,3}
like image 220
user2472340 Avatar asked Sep 16 '25 01:09

user2472340


1 Answers

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. Brace expansion
  2. Tilde expansion
  3. Parameter expansion
  4. Command substitution
  5. Arithmetic expansion
  6. Process substitution
  7. Word splitting
  8. Pathname expansion
  9. Quote removal

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[@]}"
like image 144
John Kugelman Avatar answered Sep 18 '25 18:09

John Kugelman