Question: Given the following files

```sh
$ ls
bar.txt  baz.txt  qux.txt

```

I would like to save only the first `txt` file to a variable. I tried this

```
$ var=*.txt

```

but it just saves all files

```
$ echo $var
bar.txt baz.txt qux.txt

```

I would like to do this using a wildcard if possible, and `extglob` is okay. The files here do not have spaces in the name but I would like the solution to also work for files with spaces.

After using [kamituel’s answer](https://stackoverflow.com/a/15827364) I realized that this can work too

```sh
$ set *.txt

$ echo $1
bar.txt

```

Answer: 
Use this:

```
$ var=(*.txt)
$ echo $var
bar.txt

```

Key here is to use parentheses - putting elements into array. So `echo $var` prints the first element from the array (`bar.txt`). You can see that by printing the whole array:

```
$ echo ${var[@]}
bar.txt baz.txt qux.txt

```
You can (slightly) abuse the `for` statement to set the variable:

```
$ for var in *.txt; do break; done
$ echo $var
bar.txt

```

On the plus side, `var` only contains the name of the first file, not everything matching the pattern. On the minus side, this is more verbose than the array solution that kamituel gives, and the overhead of storing all matching files is unlikely to be an issue in real life.