[Bash] Curly braces tips

Zhentiw發表於2024-05-15

For example I want to override and rename file.txt_ with file.txt

mv file.txt{_,}

You can also check the full command with echo

echo mv file.txt{_,}
# > mv file.txt_ file.txt

Another example: if you want to cat beep.txt and boop.txt file, instead of doing cat beep.txt boop.txt, you can do cat b{ee,oo}p.txt.

Using {} for loop over items in collection

echo {a,b}{c,d}
# > ac ad bc bd

generate senquence string

echo img{0..10}.png

# > img0.png img1.png img2.png img3.png img4.png img5.png img6.png img7.png img8.png img9.png img10.png

You can give step as well

echo img{0..100..10}.png

# > img0.png img10.png img20.png img30.png img40.png img50.png img60.png img70.png img80.png img90.png img100.png

Example of generate directory:

mkdir -p images/img{0..10..10}.png