12 ms·
Great suggestion, but I'd go for a middle ground: use long flags for the long tail, but short flags are fine for stuff that's very commonly used. I think I'll
by adrianmonk 6y ago
Great suggestion, but I'd go for a middle ground: use long flags for the long tail, but short flags are fine for stuff that's very commonly used.
I think I'll continue to write these:
grep -i
rm -rf
ln -s
gzip -v
sed -e
instead of:
grep --ignore-case
rm --recursive --force
ln --symbolic
gzip --verbose
sed --expression
If you're working on shell scripts, you probably know certain short flags well enough that long flags just add clutter and don't improve readability.
- gkop 6y agoLong flags aren’t just for readability, they also add entropy which contributes defensiveness against typos.
- MaxBarraclough 6y agoRelated to this, they're less likely to do something unexpected. Many programs use -v as a short flag for --version, but some (such as curl) use it as short for --verbose Probably something you'd catch pretty quickly, but still.
- dpcx 6y agoAs does Python, which drives me crazy. -V is version on Python.
- MaxBarraclough 6y agoVery annoying. Java HotSpot uses -version, rather than --version, which is even worse. Don't mess with the standard long-form flag!
- hackerm0nkey 6y agoI get it wrong every damn time. 16 years and counting.
- iforgotpassword 6y agoSame! Somehow I also start confusing this with somewhat related tools like gcc. It's rare enough that I need to query these that it's all blurry in my memory again by next time. "huh I guess gcc was that goofy one with just one dash.." Nope.
- LukeShu 6y agoTo be fair, the JVM predates GNU-style --long flags being "standard". Lots of older programs use "X11-style" -long flags (that many pre-X11 programs used, like `find`).
- deleted 6y ago[deleted]
- tannhaeuser 6y agoLong flags are a GNUism, not a "standard" by any means. The closest that comes to a standard in this space are POSIX utility conventions [1]. [1]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...
- jjgreen 6y ago... and it wrote the version to stderr up to 3.3, the stdout from 3.4; very convenient if you need to switch on version (which you do).
- nostoc 6y agoThere's so much variation in flags for version, I've taken the habit of always using the long `--version` flag.
- ben509 6y agoThe worst (well known) offender is grep -v meaning grep --invert-match just for the sheer bafflement of "verbose" now hiding what you were looking for.
- u801e 6y agoThat actually comes from ed and vi. The g command would search for a line that matches a given regular expression and run the following command on that line. The v command was the inverse (search for lines that don't match the given regular expression). The default command was to just print the line. Hence the name grep g/re/p
- EE84M3i 6y agoWow, that's funny, I normally expect '-v' to be verbose, but maybe I just use a lot of `tar` and `curl`!
- mjochim 6y agoOr maybe cp, or rm, or rsync, or mount, or wget, or netcat (both gnu and bsd) ... it’s really pretty reasonable to expect -v to mean verbose ;-).
- fuzzer37 6y agoBut also contributes to the heat death of the universe.
- edoceo 6y agowhat doesn't?
- ben509 6y agoNah, it's already happened, and we all pop up every 10^10^100 eons to pretend this is all real.
- arthurcolle 6y agoShhh don't share meta content with the NPCs...
- UI_at_80x24 6y agoAnd to add to your point, in my experience I've had "long-lived" scripts that the flags change meaning over the years. This is extremely rare on base-level installs, but frighteningly more common on the inhouse-programs I've had to deal with. The long flags are less likely to be changed over time. The script will usually fail gracefully and I don't also need to relearn what I was thinking 25 years ago when I first did it. (long flags are like in-command comments too) And as others have pointed out -v(ersion) or -v(erbose) can happen too.
- OskarS 6y agoHow often do you use gzip verbose? I don't think I've ever used it. What's it good for?
- jandrese 6y agoPretty much just to make sure you're not wasting your time with the gzip. But I also don't use gzip much these days. pbzip2 is a drop in replacement that is faster and compresses better.
- zbuf 6y agoPersonally a fan of '--verbose' to be honest. It's clear. "grep -v" anyone?
- frenchy 6y agoParticularly in this case, because `-v` sometimes means "print out the version number".
- jfrunyon 6y agoThat's why they mentioned `grep -v`. Go look it up. It doesn't mean verbose OR version. Doubt you'd be printing out a version number in a shell script though.
- cogburnd02 6y agoYou may want to compare a version number of a command to make sure it's bigger than (or equal to) the minimum version required by your script, though.
- codazoda 6y ago>-v, --invert-match > Selected lines are those not matching any of the specified patterns. For the lazy. Edit: I don't know hackernews formatting and I'm among the lazy, so... Marked as "won't fix".
- 6y ago
- ryanianian 6y agoThe short flags are really there for when you're doing things interactively. For scripts that are read and executed more often than they're written or changed, the long flags really help ensure you don't fat-finger. I'm accustomed to typing `rm -rf` when really I just need `rm -r` in a lot of cases, and in PRs it's very easy for your eyes to glaze over when there's more than a single short flag.
- jfrunyon 6y agoYou literally always need -f with -r in a script (unless you want rm to prompt the user), though...
- jrimbault 6y agoYou must have an alias rm='rm -i' lying around. rm doesn't prompt by default (unless you're the owner of the dir, but not its contents). mkdir demodir && touch demodir/{foo,bar} && rm -r demodir
- eMSF 6y agoIIRC 'rm -r' prompts by default for read-only files as well even if you own them.
- deleted 6y ago[deleted]
- jfrunyon 6y ago... huh. No, no alias, I just misread the manpage. I could've sworn that -I was the default......
- Macha 6y agoIt's a common thing for IT departments or people making enterprise images to put in because they think it makes it safer. I feel it does more harm than good by normalising rm -f when you want to recursively delete a folder, but with CD deployments these days it's less of a deal.
- redisman 6y agoI think you're assuming a level of Unix knowledge that less and less new people in the field are going to have.
- nostoc 6y agoSomeone who's tasked with maintaining shell scripts better be familiar with the shell.
- harisund 6y agothat's kinda his point. Kids today no longer know the shell. They think just writing some yaml files in ansible is sufficient. Giving them a shell script won't help at all.
- nobody9999 6y ago>Kids today no longer know the shell. Sounds like Kochan and Wood[0] need some love (and royalties). Or is learning stuff deprecated these days? [0] https://www.amazon.com/Unix-Shell-Programming-Stephen-Kochan/dp/0672324903 https://www.amazon.com/Unix-Shell-Programming-Stephen-Kochan...
- nemetroid 6y agoI agree. To me, "sed -e" is an expression as a whole, and replacing -e with --expression is on the same level as aliasing "sed" to "stream-editor". Though I would make the list of "allowed" short flags very short. I can't think of many more than the ones you listed: mkdir -p sh -c cp -r tar -xaf tar -caf
- lukeschlather 6y agotar is one of the worst. I think I know what those do without looking it up but I think most people look at that with bewilderment. Randall Munroe is always helpful in these matters and has this to say: https://xkcd.com/1168/ https://xkcd.com/1168/
- UI_at_80x24 6y agoI remember 90% of the time which flag I want to use, but the other 10% sends me into a rage! =) I gave up and added this to my .bashrc: function extract() { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *.zst) zstd -d $1 ;; *.xz) unxz $1 ;; *) echo "'$1' cannot be extracted via >extract<" ;; esac else echo "'$1' is not a valid file" fi }
- thequux 6y agoRecent (as in, within the last ~3-5 years) versions of GNU tar automatically detect the type of compression and apply the appropriate tool, so you can often get away with `tar xvf`.
- 6y ago
- nooyurrsdey 6y agoWhile i agree these are "common" you now have to define what common flags are. It's easier for an organization to just have a blanket rule to use long form expressions Also is typing that much of a chore?
- lcuff 6y agoI argue that 'very commonly used' is subjective, and someone new won't know all the ones you or I consider common. One of my goals in writing software is to make my code understandable to complete newcomers. I can't always achieve it. Sometimes other design goals take priority. But from my perspective this one is a no-brainer. I just don't see the down side. If it's too time consuming to type, use them for typing practice and get faster :-)
- matheusmoreira 6y ago> If you're working on shell scripts, you probably know certain short flags well enough that long flags just add clutter and don't improve readability. What about other people who might read the script?
- webreac 6y agoI have more difficulties to read long flags. The people who want to improve by reading my scripts will have to search for the short flags because nobody use long flags from command line. IMHO the benefits of long flags vs short ones are not obvious.
- leoh 6y agoI disagree. BSD and GNU implementations differ. Because of this, it's easier to discern intent of long flags.
- 0xdeadb00f 6y agoAlso, some BSD equivalents of GNU commands don't even have long flags.
- leoh 6y agoThat's not the point. The point is that you can discern intent if you are writing against the GNU implemention so one can trivially reimplement against BSD.
- scbrg 6y agoI assume the intent to discern, then, is that the script isn't supposed to run on BSD :-)
- michaelcampbell 6y agoIndeed. Almost all arguments are "readability" are only relevant to a given reader. Everything is hard to read until you learn to read it.
- anjanb 6y agoI would agree. Especially when you have something like https://explainshell.com/ https://explainshell.com/ to help you understand a command and its flags. It would be nice to have editor/IDE extensions which can do equivalent of explainshell.com's functionality.
- lhorie 6y agoI dunno. I may know them but the next person may not. Where I work, there are plenty of people who primarily work w/ other langs and only dabble in bash once in a blue moon. Case in point, when I was learning, I'd copy and paste snippets I found online without understanding what some flags did. At first I didn't even know how to look up docs, and googling for the meaning of shorthand isn't always fruitful, especially given the "don't know how to find docs" limitation. I've even ran into cases where the next person is myself. For example at one point, I "knew" docker flags while it was fresh in my mind, but then forgot what they meant a few months later...
- rgoulter 6y agoYes. Suggestions are good. Following suggestions with purpose is better. Or, rather, blindly following suggestions without understanding the benefits (and costs to) the suggestions doesn't seem ideal to me. -- It respects the great answer of "it depends". Gonna admit that I wouldn't be able to tell what grep -i, sed -e do. (I would've used `sed -e` before, but not enough to remember that). Still, some things are clear from context.