Live data from Hacker News

Use long flags when scripting (2013)

changelog.com

81–90 of 197 posts

Re: Use long flags when scripting (2013)

#82

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 flag…

I 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

tar 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/

Re: Use long flags when scripting (2013)

#83
post #63

I have been following this advice for about five years with my scripts and the best result has been compliments from co-workers and others who have picked up the scripts and felt like they had a much better understanding of what the scripts were doing. Combined with use of shellcheck and shfmt working in shell scripting has come a long way in the last couple years. I still feel like BASH is a dead end--the quoting an…

It's so unfortunate about Bash + Apple.

Bash probably deserves to die out, but Apple has really pulled a bait-and-switch on OSX's *nix/bsd underpinnings over the years.

Re: Use long flags when scripting (2013)

#84
post #78

Earlier quoted context omitted.

Related 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.

As does Python, which drives me crazy. -V is version on Python.

Very annoying. Java HotSpot uses -version, rather than --version, which is even worse. Don't mess with the standard long-form flag!

Re: Use long flags when scripting (2013)

#85
post #71

Earlier quoted context omitted.

It'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.

Yeah... no. Also definitely not the case. If I actually am remembering it correctly then it's probably from Fedora circa FC5 or Ubuntu circa 6.06. Back when I was in school and had never touched Linux other than on my own computer.

Go do a fresh install of CentOS/RHEL (or just spin up a new docker container) and what do you see in root's bashrc?

    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
I've also encountered one job which adds this to their base ubuntu image, and my current employer uses Macs which have it added to bashrc by their mdm software on initial install.

So what's your justification to dismiss the idea that this is a common practice?

Re: Use long flags when scripting (2013)

#86
post #21

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 flag…

Long flags aren’t just for readability, they also add entropy which contributes defensiveness against typos.

And 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.

Re: Use long flags when scripting (2013)

#88
post #85

Earlier quoted context omitted.

Yeah... no. Also definitely not the case. If I actually am remembering it correctly then it's probably from Fedora circa FC5 or Ubuntu circa 6.06. Back when I was in school and had never touched Linux other than on my own computer.

Go do a fresh install of CentOS/RHEL (or just spin up a new docker container) and what do you see in root's bashrc? alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' I've also encountered one job which adds this to their base ubuntu image, and my current employer uses Macs which have it added to bashrc by their mdm software on initial install. So what's your justification to dismiss the idea that this is a common pr…

Aliases shouldn't be run in non-interactive shells IIRC.

Re: Use long flags when scripting (2013)

#89

Earlier quoted context omitted.

I 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

tar 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/

I 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

Re: Use long flags when scripting (2013)

#90
post #78

Earlier quoted context omitted.

As does Python, which drives me crazy. -V is version on Python.

Very annoying. Java HotSpot uses -version , rather than --version , which is even worse. Don't mess with the standard long-form flag!

I get it wrong every damn time. 16 years and counting.
Post reply on HN