Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange grammar in cat command

Tags:

bash

cat

First of all, I apologize if this question can be answered with a web search, but I couldn't find anything.

There is some grammar in the cat command which I've seen to "repeat" files.

cat file{,}

Is equivalent to calling

cat file file

Also,

cat file{,}{,}{,}{,}

repeats file not four times, but 16 times. In addition,

cat file{,,}

repeats file 3 times.

I would like to know more about this grammar. What is it called? Is it built into cat or is it a shell feature? Are there more features of this grammar?

like image 348
tomKPZ Avatar asked Jan 22 '26 12:01

tomKPZ


1 Answers

This feature is called brace expansion.

Generally you can write file{1,2,3} and bash expands it to file1 file2 file3 before running the command.

If you write

mkdir foo{1,2,3}{a,b}

it will be equivalent to

mkdir foo1a foo1b foo2a foo2b foo3a foo3b

and will create those 6 directories.

In your case ({,}) you are adding nothing and nothing and therefore get the same word twice.

like image 180
michas Avatar answered Jan 25 '26 11:01

michas