Stories
Slash Boxes
Comments

SoylentNews is people

posted by martyb on Tuesday January 23 2018, @09:52AM   Printer-friendly
from the how-many-did-you-already-know? dept.

Explore some of the more useful but perhaps more esoteric capabilities of the Bash shell with the blog post Ten More Things I Wish I'd Known About bash. It is a followup to the highly visible post by the same author on Ten Things I Wish I'd Known About bash. Modern shells like Bash, Ksh, and Zsh have over four decades of developent and refinement, making them powerful, flexlble, and fast user interfaces for efficient work — not just excellent scripting languages for automation.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
(1)
  • (Score: 2) by turgid on Tuesday January 23 2018, @10:00AM (3 children)

    by turgid (4318) Subscriber Badge on Tuesday January 23 2018, @10:00AM (#626482) Journal
    • (Score: 3, Funny) by DannyB on Tuesday January 23 2018, @02:46PM (2 children)

      by DannyB (5839) Subscriber Badge on Tuesday January 23 2018, @02:46PM (#626564) Journal

      #6: These are very handy for knocking up scripts.

      I didin't know scripts could get pregnant.

      --
      What doesn't kill me makes me weaker for next time.
      • (Score: 5, Funny) by Anonymous Coward on Tuesday January 23 2018, @05:25PM (1 child)

        by Anonymous Coward on Tuesday January 23 2018, @05:25PM (#626643)

        Sure they can. How do you think they get child processes?

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @04:16AM

          by Anonymous Coward on Wednesday January 24 2018, @04:16AM (#626961)

          You fork the forkers, that's how.

  • (Score: 2) by PiMuNu on Tuesday January 23 2018, @10:52AM (21 children)

    by PiMuNu (3823) on Tuesday January 23 2018, @10:52AM (#626499)

    ...ten more things it is easy to do in any modern scripting language; but you never even considered trying in bash (or any shell script).

    • (Score: 5, Insightful) by turgid on Tuesday January 23 2018, @11:08AM (7 children)

      by turgid (4318) Subscriber Badge on Tuesday January 23 2018, @11:08AM (#626508) Journal

      "Gluing things together with a shell script is the Unix way." Gilmour/Waters.

      • (Score: -1, Offtopic) by Anonymous Coward on Tuesday January 23 2018, @12:04PM

        by Anonymous Coward on Tuesday January 23 2018, @12:04PM (#626522)

        do you know de wey

      • (Score: 1, Interesting) by Anonymous Coward on Tuesday January 23 2018, @01:46PM (5 children)

        by Anonymous Coward on Tuesday January 23 2018, @01:46PM (#626542)

        I have yet to see any significant software that is the product of unix processes glued together by shell code; it's only been things like configuration scripts or for interactive, "exploratory" programming.

        If you wanted to use a scripting language that called out to unix processes that also did real work on its own, you'd have to step up to perl or python.

        • (Score: 3, Informative) by turgid on Tuesday January 23 2018, @03:24PM

          by turgid (4318) Subscriber Badge on Tuesday January 23 2018, @03:24PM (#626572) Journal

          Someone needs to read the Unix Koans of Master Foo.

        • (Score: 3, Funny) by Nerdfest on Tuesday January 23 2018, @04:09PM

          by Nerdfest (80) on Tuesday January 23 2018, @04:09PM (#626598)

          15 years ago, the internet would like a word with you. Hell even now, but to a lesser degree.

        • (Score: 2) by fyngyrz on Tuesday January 23 2018, @10:11PM

          by fyngyrz (6567) on Tuesday January 23 2018, @10:11PM (#626799) Journal

          All I need to know about whatever command shell is running:

          • For some cases, how to prevent a command shell window from opening (Windows... bah)
          • How to pass parameters to it on the command line without them getting interpreted or expanded
          • How to catch those parameters, unmolested, inside the command shell
          • How to pass those parameters to Python
          • Profit!

          And bingo, now I have one set of scripting knowledge that works with Windows, OSX, and linux. And it's hella powerful.

          Ideally, I just set a Python script to be executable and run it directly. Or, if a shell command is the only option because... reasons... then what it does is launch a Python script.

          It makes no sense to me to use up brainspace with shell esoterica these days.

        • (Score: 3, Informative) by urza9814 on Tuesday January 23 2018, @10:58PM

          by urza9814 (3954) on Tuesday January 23 2018, @10:58PM (#626839) Journal

          I have yet to see any significant software that is the product of unix processes glued together by shell code; it's only been things like configuration scripts or for interactive, "exploratory" programming.

          I work for a Fortune 10 company. Our core application has a few hundred shell scripts running every night for batch processing, particularly for data coming in from external sources. Various third party companies SFTP a data file, we validate and do some processing of it with a shell script, then we trigger sql or some other process to load into the database if needed. We do the same thing for data output, and that includes output to numerous state and federal regulatory agencies, so it seems to be a pretty typical way of doing these things. In total we've got around half a million lines of bash/ksh running in production...which I'm sure is nothing compared to our application level Java code, but it's still a hell of a lot more than just "configuration scripts or exploratory programming".

        • (Score: 2) by http on Thursday January 25 2018, @07:23AM

          by http (1920) on Thursday January 25 2018, @07:23AM (#627584)

          I have worked at a place where customer managment was (i) written in bash, and (ii) run as CGI. Working there taught me almost as much about bash as lhunath's guide. Maybe not the fastest software, but being a small shop, we never had more than three accounts being created or modified at the same time.

          Billing, however, was done in perl. Speed matters in some situations.

          --
          I browse at -1 when I have mod points. It's unsettling.
    • (Score: 3, Informative) by canopic jug on Tuesday January 23 2018, @11:40AM (7 children)

      by canopic jug (3949) Subscriber Badge on Tuesday January 23 2018, @11:40AM (#626513) Journal

      Let me guess, all your sessions default to xonsh [xonsh.org] instead of bash or zsh?

      --
      Money is not free speech. Elections should not be auctions.
      • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @12:46PM (4 children)

        by Anonymous Coward on Tuesday January 23 2018, @12:46PM (#626528)

        I never heard of that. I love the first line of their table, "Sane language" ... bash has no tick

        OTOH, I clicked on the xonsh "tutorial" link and got an empty page. Oh dear.

        • (Score: 2) by tangomargarine on Tuesday January 23 2018, @03:56PM (1 child)

          by tangomargarine (667) on Tuesday January 23 2018, @03:56PM (#626589)

          Xonsh is a Python-ish, BASHwards-looking shell language and command prompt.

          Please tell me "BASHwards-looking" isn't a phrase anybody actually uses. If so, these guys should be slapped for such a crime against the English language.

          --
          "Is that really true?" "I just spent the last hour telling you to think for yourself! Didn't you hear anything I said?"
          • (Score: -1, Redundant) by Anonymous Coward on Tuesday January 23 2018, @08:10PM

            by Anonymous Coward on Tuesday January 23 2018, @08:10PM (#626724)
        • (Score: 1, Funny) by Anonymous Coward on Tuesday January 23 2018, @05:29PM

          by Anonymous Coward on Tuesday January 23 2018, @05:29PM (#626646)

          OTOH, I clicked on the xonsh "tutorial" link and got an empty page.

          See? It's so easy, even an empty tutorial will do. ;-)

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @08:35AM

          by Anonymous Coward on Wednesday January 24 2018, @08:35AM (#627055)

          ☐ Sane web development practices

      • (Score: 1, Interesting) by Anonymous Coward on Tuesday January 23 2018, @08:35PM (1 child)

        by Anonymous Coward on Tuesday January 23 2018, @08:35PM (#626736)

        fish

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @09:13AM

          by Anonymous Coward on Wednesday January 24 2018, @09:13AM (#627060)

          https://fishshell.com/ [fishshell.com]

    • (Score: 3, Insightful) by Snotnose on Tuesday January 23 2018, @04:36PM (4 children)

      by Snotnose (1623) on Tuesday January 23 2018, @04:36PM (#626614)

      I've been using Unix since before most of these scripting languages existed. It always takes me by surprise people don't know this stuff.

      When I started you had sh, sed, awk, lexx and yacc for scripting. Tcsh, perl, etc etc etc came much later.

      / that's my lawn you're on
      // I put a lot of work into maintaining it
      /// Hope you're enjoying it, if you have a picnic I want a piece of fried chicken

      --
      When the dust settled America realized it was saved by a porn star.
      • (Score: 3, Interesting) by bzipitidoo on Wednesday January 24 2018, @12:00AM (2 children)

        by bzipitidoo (4388) on Wednesday January 24 2018, @12:00AM (#626865) Journal

        I started on Apple DOS 3.3 and MSDOS. Apple DOS is amazingly primitive-- doesn't even have a native "copy" command, instead has this utility program they called FID to do various file operations, including copy. MSDOS was much more capable, if not as powerful as bash. But it sure can be a lot easier to use. In bash I frequently do "for i in *.ext; do some_command $i ${i%.*}.foo; done" when I used to do "some_command *.ext" in MSDOS, and it worked, because so often the DOS version of the command was a little smarter about generating an appropriate name for the output file.

        Heck, it's only recently that I learned of ${i%.*}. Used to use ${i:0:$((${#i}-4))}, which assumes the extension plus dot is 4 characters.

        I've tried and tried to grok bison/yacc and flex/lex, but no luck. Bison hates my grammars, always complains of shift/reduce errors. It was easier to bang out a state machine in C by hand than fool endlessly with those utilities.

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @12:40AM

          by Anonymous Coward on Wednesday January 24 2018, @12:40AM (#626880)

          xargs - learn to use it. xe is nice as well

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @12:42AM

          by Anonymous Coward on Wednesday January 24 2018, @12:42AM (#626881)

          ${i:0:$((${#i}-4))}

          yeech, I'd pipe it through bloody sed first.

      • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @04:24AM

        by Anonymous Coward on Wednesday January 24 2018, @04:24AM (#626962)

        lex/yacc are lexer/parser generators. What the hell are you doing with it in scripting?

  • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @01:21PM (15 children)

    by Anonymous Coward on Tuesday January 23 2018, @01:21PM (#626532)

    You have to write software forever and all you have is the unix shells... and the scripts keep getting larger... larger... (no perl, that is a different hell--maybe purgatory)

    • (Score: 3, Insightful) by DannyB on Tuesday January 23 2018, @02:53PM (12 children)

      by DannyB (5839) Subscriber Badge on Tuesday January 23 2018, @02:53PM (#626566) Journal

      In hell . . .
      * you get Usenet, but there is only one newsgroup: alt.soc.rec.talk.comp.news.sci.misc.
      * You only get Windows 3.1
      * On an 8 MHz 80386
      * The only programming language is Perl
      * The only shell is command.com
      * The only editor is edlin (that would stop the fighting about vi vs emacs and make people thankful for the hard work that went into both!)
      * A whopping 2 GB hard drive!
      * For only $7995!
      * And 9600 Kbps dialup!

      --
      What doesn't kill me makes me weaker for next time.
      • (Score: 4, Informative) by Freeman on Tuesday January 23 2018, @04:30PM

        by Freeman (732) on Tuesday January 23 2018, @04:30PM (#626612) Journal

        2GB is too large. 80MB would be much more realistic for the hardware you're talking about. Even then, that would have been top of the line.

        --
        Joshua 1:9 "Be strong and of a good courage; be not afraid, neither be thou dismayed: for the Lord thy God is with thee"
      • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @05:21PM (4 children)

        by Anonymous Coward on Tuesday January 23 2018, @05:21PM (#626640)

        I could deal with all of that except edlin and command.com. I feel my tourrettes acting up again, gotta go.. ;)

        • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @05:39PM (3 children)

          by Anonymous Coward on Tuesday January 23 2018, @05:39PM (#626658)

          Given that you have Perl, it should be easy to avoid using command.com almost completely.

          • (Score: 3, Insightful) by turgid on Tuesday January 23 2018, @07:43PM

            by turgid (4318) Subscriber Badge on Tuesday January 23 2018, @07:43PM (#626715) Journal

            And I'm sure you could re-implement vi in a dozen lines of Perl.

          • (Score: 2, Funny) by Anonymous Coward on Tuesday January 23 2018, @08:13PM (1 child)

            by Anonymous Coward on Tuesday January 23 2018, @08:13PM (#626726)

            Perl also turns the f$!#%ing tourettes into a programming skill.

            • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @01:30AM

              by Anonymous Coward on Wednesday January 24 2018, @01:30AM (#626897)

              ROFL - best comment of the day

      • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @02:33AM (2 children)

        by Anonymous Coward on Wednesday January 24 2018, @02:33AM (#626924)

        That must be with the Turbo button disengaged.
        The slowest 386 was 16MHz.

        -- OriginalOwner_ [soylentnews.org]

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @09:33AM (1 child)

          by Anonymous Coward on Wednesday January 24 2018, @09:33AM (#627062)

          The first ones were rated at 12 MHz.

          • (Score: 1) by toddestan on Saturday January 27 2018, @09:15PM

            by toddestan (4982) on Saturday January 27 2018, @09:15PM (#629145)

            The only reason to go with the 386 is to be able to run Windows 95. Which was sluggish enough on a 486, and had to be complete molasses on a 386. Windows 3.1 ran okay on a 386, and would even run acceptably well on a 286.

            Of course, that assumes the luxury of a hard drive. My first PC did not have a hard drive, but it did have 2 floppy drives which was more than what some people had.

      • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @10:12AM (1 child)

        by Anonymous Coward on Wednesday January 24 2018, @10:12AM (#627069)

        2GBs my word. My 7MHz XT had 21 megs I had reams of games and business applications and three different OS / shell variants and I don't think my hdd utilisation ever reached 25%

        • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @10:14AM

          by Anonymous Coward on Wednesday January 24 2018, @10:14AM (#627070)

          Also, none of that sounds so bad. Windows 3.1 is a helluva lot better than windows 95 and you gave me perl and an internet connection so I can make that machine do anything that is worth doing. Although on those specs i'd probably just stick with DOS.

      • (Score: 0) by Anonymous Coward on Wednesday January 24 2018, @09:40PM

        by Anonymous Coward on Wednesday January 24 2018, @09:40PM (#627400)

        You made me feel old. I remember Windows 3.0. I remember editing with debug.com (a DOS command). I don't remember how, but I remember doing it. My first dialup connection from home was over a 1200 baud modem with no AT commands. I had to dial with my phone when establishing a connection--both ways, in the snow.

    • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @05:37PM

      by Anonymous Coward on Tuesday January 23 2018, @05:37PM (#626656)

      You have to write software forever and all you have is the unix shells... and the scripts keep getting larger... larger... (no perl, that is a different hell--maybe purgatory)

      As long as I get to write all the shell scripts myself, and have access to all necessary documentation and enough time for development, I don't see a problem.

      Of course, being hell, it will probably be a big heap of existing, badly written scripts to modify, not enough time to even understand one of them, and no accessible documentation for anything.

    • (Score: 1) by rootsquasher on Tuesday January 23 2018, @06:30PM

      by rootsquasher (6851) on Tuesday January 23 2018, @06:30PM (#626681)
      perl, that is a different hell

      So true. I would take bash over perl any day.
  • (Score: 5, Informative) by Anonymous Coward on Tuesday January 23 2018, @02:21PM

    by Anonymous Coward on Tuesday January 23 2018, @02:21PM (#626554)

    Don't use $RANDOM to create temporary files, as you're not guaranteed to get a unique number. Use the 'mktemp' command instead.

    eg.
    tmp_file=$(mktemp)

    if you need a temporary directory:

    tmp_dir=$(mktemp -d)

  • (Score: 2) by Rich on Tuesday January 23 2018, @03:05PM (5 children)

    by Rich (945) on Tuesday January 23 2018, @03:05PM (#626570) Journal

    ...starts with the same two letters as "shell". There you have it. That's what shells with their scripting are. UNIX shell acts completely unpredictable to someone who has not used it daily for years. And if someone is made to use it daily for years, it's a waste of development resources that couldn't be matched in late-stage socialist countries.

    • (Score: 4, Touché) by turgid on Tuesday January 23 2018, @03:25PM (3 children)

      by turgid (4318) Subscriber Badge on Tuesday January 23 2018, @03:25PM (#626573) Journal

      This level of ignorance ensures that there will always be gainful employment for those who can give the illusion of working magic.

      • (Score: 3, Insightful) by Rich on Tuesday January 23 2018, @07:21PM (2 children)

        by Rich (945) on Tuesday January 23 2018, @07:21PM (#626701) Journal

        The leper in a leper colony will regard leprosy as something perfectly normal. It's not that shell magic is not understandable, just as leprosy is well understood by those with medical knowledge. (Also remember the scene in "Princess Mononoke", where Lady Eboshi employs the lepers as gunsmiths, very useful! ... :P)

        Shell is just uneconomical, or at least annoying, to get right, because its expressive basics are fundamentally broken. How often did I come across scripts that broke with spaces in filenames? And it gets especially bad, if the target has to be classical "sh", without "ba", "z", "k" or "da" in front of it.

        I, for one, much prefer Python as digital duck tape.

        • (Score: 4, Insightful) by turgid on Tuesday January 23 2018, @07:40PM

          by turgid (4318) Subscriber Badge on Tuesday January 23 2018, @07:40PM (#626713) Journal

          How often did I come across scripts that broke with spaces in filenames?

          Spaces in file names are an abomination. Anyone who puts spaces in file names is a filthy pervert. The Good Lord gave us the underscore.

          And it gets especially bad, if the target has to be classical "sh", without "ba", "z", "k" or "da" in front of it.

          It helps to know what language you're writing in when you try to write a program.

          I, for one, much prefer Python as digital duck tape.

          Youth, as the great man said, is wasted on the young. Quack.

        • (Score: 3, Insightful) by bob_super on Tuesday January 23 2018, @10:07PM

          by bob_super (1357) on Tuesday January 23 2018, @10:07PM (#626796)

          > How often did I come across scripts that broke with spaces in filenames?

          How often did you get STDs from unprotected sex with popular prostitutes?

    • (Score: 2) by bzipitidoo on Wednesday January 24 2018, @12:16AM

      by bzipitidoo (4388) on Wednesday January 24 2018, @12:16AM (#626872) Journal

      If that was really how the term "shell" was derived, it makes it a portmanteau of "shit" and "hell".

      Could be worse. Could have been a bit of oil company propaganda, I mean, imagine if the languages Perl, Python, and PHP were instead named Chevron, Exxon, and BP.

  • (Score: 1) by negrace on Tuesday January 23 2018, @03:32PM (4 children)

    by negrace (4010) on Tuesday January 23 2018, @03:32PM (#626576)

    1. Python
    2. Python
    3. Python
    4. Python
    5. Python
    6. Python
    7. Python
    8. Python
    9. Python
    10. Python

    • (Score: 2) by BsAtHome on Tuesday January 23 2018, @03:50PM

      by BsAtHome (889) on Tuesday January 23 2018, @03:50PM (#626586)

      That will get the reply - ten times Hiss, which is what snakes do. Therefore, while we are at the sssss, why not do it properly shhhhh and then more conveniently, bash. Case solved.

    • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @04:57PM

      by Anonymous Coward on Tuesday January 23 2018, @04:57PM (#626622)

      I have been using Octave for most things beyond trivial bash one-liners. It's way too heavy... but then so is Python :)

    • (Score: 1, Insightful) by Anonymous Coward on Tuesday January 23 2018, @05:01PM

      by Anonymous Coward on Tuesday January 23 2018, @05:01PM (#626629)

      You put a tab in there somewhere and blew up your smarmy post. Or was it a space? I can't tell because I can't see it. Oh well.

    • (Score: 2) by turgid on Wednesday January 24 2018, @10:50AM

      by turgid (4318) Subscriber Badge on Wednesday January 24 2018, @10:50AM (#627082) Journal

      I thought you were going to say C++ there for a minute.

  • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @03:45PM

    by Anonymous Coward on Tuesday January 23 2018, @03:45PM (#626584)

    I was going to do something fancy to fix a bunch of photos that had the same name. Then I just moved everything into iCloud and paid the fee.

  • (Score: 4, Informative) by mmh on Tuesday January 23 2018, @04:42PM (2 children)

    by mmh (721) on Tuesday January 23 2018, @04:42PM (#626618)

    Author put in an extra, uneeded, caret. You only need two:

    $ echo $SHELL
    /bin/bash
    $ echo hllo
    hllo
    $ ^ll^ell
    echo hello
    hello

    And, imho, I think the author missed two of the best shortcuts in bash:

    1: ctrl+r = reverse history search, press ctrl+r, type any part of a previously run command. press ctrl+r to keep going back through the history.

    2: alt+. = copy and paste the last argument from the last command. e.g.:

    $ mkdir test/
    $ cd <alt+.>

    • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @08:46PM (1 child)

      by Anonymous Coward on Tuesday January 23 2018, @08:46PM (#626746)

      $ mkdir test
      $ cd !$

      • (Score: 1) by mmh on Tuesday January 23 2018, @09:15PM

        by mmh (721) on Tuesday January 23 2018, @09:15PM (#626764)

        alt+. is 2 keys, !$ is 4. It's like golf man.

  • (Score: 0) by Anonymous Coward on Tuesday January 23 2018, @05:18PM

    by Anonymous Coward on Tuesday January 23 2018, @05:18PM (#626638)

    What nix programmer doesn't know these or the awfully prosaic original 10?

  • (Score: 4, Interesting) by darkfeline on Tuesday January 23 2018, @06:53PM (3 children)

    by darkfeline (1030) on Tuesday January 23 2018, @06:53PM (#626691) Homepage

    Another way of saying "I didn't read the manual and didn't realize all of these useful features exist, and I'm proud of my ignorance!" Go read the Bash manual, it's not that long and contains all of these and more.

    Hell, go read the POSIX shell specification, it's even shorter and contains a third of these. If you don't know the POSIX shell spec, you have no business writing a shell script.

    --
    Join the SDF Public Access UNIX System today!
    • (Score: 2) by DannyB on Tuesday January 23 2018, @09:32PM

      by DannyB (5839) Subscriber Badge on Tuesday January 23 2018, @09:32PM (#626775) Journal

      So it's like . . .

      Who knew that health care was complicated?

      Or Ten Things I Wish I Knew About Being President.

      --
      What doesn't kill me makes me weaker for next time.
    • (Score: 3, Funny) by bzipitidoo on Wednesday January 24 2018, @12:28AM (1 child)

      by bzipitidoo (4388) on Wednesday January 24 2018, @12:28AM (#626876) Journal

      Do you know how to write syntax files for vim? Why not?? Come on, it's only 10 pages of text to read through in the vim help!

      • (Score: 2) by darkfeline on Wednesday January 24 2018, @06:39PM

        by darkfeline (1030) on Wednesday January 24 2018, @06:39PM (#627278) Homepage

        No, because Vim is for losers. I use ed, vi, and emacs where appropriate, and I have read the manuals for each.

        --
        Join the SDF Public Access UNIX System today!
(1)