It is currently Sat Dec 21, 2024 7:27 pm

The Art of Command Line

If you have something technology-related to share and don't feel like cluttering up General Chat, post it here. Anything is fair game and anything highly technical is preferred.

The Art of Command Line

Postby Zancarius » Tue Jun 16, 2015 9:28 am

https://github.com/jlevy/the-art-of-com ... term=daily

This is useful if you want to be a cli junky like me (although my preference is to use zsh, not bash). I haven't read the entire thing yet, but it seems to contain a lot of useful information especially for someone just starting out with *nix.

Reading through it, I'm reminded of a curious bit of convergent evolution. Story time!

The globbing operator (or "*" to those of you who prefer symbols) has some interesting history in that it evolved under both Unix shells and the MSDOS command line. I'm not entirely sure if it first appeared under the shell and was "borrowed" by DOS (which I guess would make this divergent evolution), but the behavior between the two is different in ways that might perplex the casual user.

Firstly, if you've ever attempted to remove a large number of files from a directory using rm * under the shell, you've no doubt been surprised to learn that you can't. In some cases, the only way to do so us to either use find and xargs or, depending on your shell, sometimes a for loop can save you from a small amount of frustration. Yet the same thing will work under DOS in most cases. Why?

Well, as I'm sure you'll read in the link (above), the globbing operator under the shell is actually substituted with the names of all files in the directory matching the glob pattern (or current directory if you just used the glob operator by itself). Some tools, like rm, will fail with large directories because there's a limit to the number of command line arguments they'll accept, occasionally as low as 1024, but this value differs between systems, libc versions, and so forth. In DOS parlance it should normally work because the globbing is done by the application, not the shell.

It may seem that the DOS solution is the superior one since the application itself derives a list of files matching the argument pattern and does something with it. But bear in mind that this also implies that each application must implement the glob on its own, and if it doesn't, then the application is likely to behave differently than you might expect. In my experience, it makes far more sense for the shell to perform expansion rather than the application, but it's a good illustration of design differences that have appeared over the years for better or worse.

Of course, you're unlikely to ever need to be aware of this nonsense with DOS syntax since almost no one ever uses it these days, and most people who are heavy into shell use in Windows either install mingwin, cygwin, or use PowerShell.
I gave that lich a phylactery shard. Liches love phylactery shards.
User avatar
Zancarius
Site Admin
 
Posts: 3907
Joined: Wed Jul 05, 2006 3:06 pm
Location: New Mexico
Gender: Male

Return to Technology Lounge

Who is online

Users browsing this forum: No registered users and 5 guests