Tuesday 30 July 2019

Use of * in bash

Bash will parse and expand special args before your commands.

Hence echo * will list files in your dir like ls.


Consider the following interresting case copied from SO. A directory has these contents:
  • test (regular file)
  • test1 (directory)
  • test2 (directory)
  • test3 (directory)
If you then type `mv *` something seemingly weird happens: test3 is there, but the rest is gone. While weird at first, it makes sense once you understand what bash actually passes to `mv`. Because of the asterisk, bash interprets mv * as mv test test1 test2 test3, and when mv gets that list, it'll assume that the last arguement is the destination, which is where all of the files would've been moved.

No comments:

Post a Comment