News for hackers. Stuff that matters.
Re:Something about functions in Bash (2017) by hyperpape on Thursday July 05, 2018 @03:38PM (#17466161)
I've thought about this a lot, but assuming you want to be pleasant for interactive use, you're subject to a tremendous number of constraints. Just to start with:

    rm foo bar
    rm -rf *
must be invocations of the rm command with the appropriate arguments.

You need bare strings, you need variable interpolation, you need redirects.

I think there's some room to break a few idioms and make something nicer, but it really is a hard problem. The choice to break things has to be based on how common an idiom is, how painful it is to not use it, and how much benefit you can get.

Re:Something about functions in Bash (2017) by laumars on Thursday July 05, 2018 @05:02PM (#17466821)
I think you've hit the nail on the head there. I've been experimenting with writing my own shell and the original syntax was a lot more like Javascript but having to encapsulate parameters in parentheses, comma delimited and quotation was a massive pain in the arse. So I dropped the requirement for quotation marks, then made commas optional, then I dropped using periods to pipe (ala methods in OOP) and then eventually ended up rewriting the entire parser to follow a more traditional POSIX syntax. As it turns out, as ugly as Bash et al is, it actually makes a lot of sense for writing one liners in an interactive shell (as you stated yourself)

So where I decided to deviate was areas I felt could be enhanced without breaking the POSIX syntax too significantly:

* shell configuration,

* error handling (something traditional shells are appallingly bad at)

* and support for complex data structures (eg so you can grep through items in a minified JSON array as smartly as you can with a traditional stream of lines.

I always respected the power of POSIX shells even before embarking on my pet project. However I never quite appreciated just how sane it's ugly syntax was until I attempted to replace it with something more readable. I mean sure there are still some specific areas I really don't agree with but that can always be argued as personal preference.

As an aside note, one thing I didn't quite appreciate until writing my shell is just how much shells have in common with functional programming. Yes I know it's a far cry from LISP machines; but if you take away the variable interpolation and environmental variables then you're left with a functional pipeline that take something from STDIN and write it to STDOUT and STDERR and does so in a multi-threaded, concurrent, workflow by default. Weirdly this still makes POSIX shells more efficient for some types of data processing than writing a monolithic routine in a more powerful (or should that be "more descriptive"?) programming language.

So there is some surprising elegance amongst all that ugliness.

Re:Something about functions in Bash (2017) by greenyouse on Thursday July 05, 2018 @07:37PM (#17467798)
On the data processing with bash part, Joyent Manta always looked interesting[0]. Reading about data processing via bash makes it sound pretty fun.

[0]https://github.com/joyent/manta/blob/master/README.md

Re:Something about functions in Bash (2017) by madmulita on Friday July 06, 2018 @09:01AM (#17471018)
Why don't we have something similar to vi modes to jump between different 'interpretations'? I believe scsh had something to that effect using chars to switch modes ala quote/unquote.

Maybe something based on Color Forth's ideas?

Re:Something about functions in Bash (2017) by laumars on Friday July 06, 2018 @09:08AM (#17471062)
Because that wouldn't work for copying and pasting code nor writing shell scripts. If you're going down that route then really you need a printable character to prefix the command line (or similar solution)

For what it's worth though, most shells (including my own one) do support switching between different text entry / hotkey modes - such as vi - even if the syntax is consistent.

Re:Something about functions in Bash (2017) by vram22 on Friday July 06, 2018 @03:11PM (#17474267)
So how is your shell coming along? Are you planning to release it as open source, or at all? I remember you mentioning it a few times before on HN.
Re:Something about functions in Bash (2017) by laumars on Friday July 06, 2018 @07:03PM (#17475784)
Pretty well thank you. I now use it everyday as my primary shell, but the documentation isnt where I'd want it yet so I can imagine it would trip new users up in areas I've chosen to break from POSIX / "Bashisms". There are also a few areas of subtle bugs I'm still running into, but that is to be expected with anything on this kind of scale.

It is already open source but I'm the only contributer currently. Which is fine as it's a personal project anyway. So anything beyond that is a bonus.

The readline API I wrote for it is pretty nifty though. I'm thinking of spinning that off into its own repo since I've been a little disappointed with the existing realine APIs for Go so I think other people might genuinely benefit from that even if they're not particularly interested in a semi-Bash compatible alternative $SHELL.

Re:Something about functions in Bash (2017) by vram22 on Saturday July 07, 2018 @07:46PM (#17481318)
Cool. Good luck with it. Interesting about the readline stuff. Can you share the link to the shell project? I'd like to look at the code, with a view to learning something about the techniques involved, as command-line tools and shells is an interest of mine.
Re:Something about functions in Bash (2017) by laumars on Sunday July 08, 2018 @07:29AM (#17483264)
Thank you.

The source can be found at https://github.com/lmorg/murex

Happy to discuss any questions or comments you might have on it.

Re:Something about functions in Bash (2017) by vram22 on Sunday July 08, 2018 @11:38AM (#17484288)
Thanks. Will check out the code over a period, and ask you if I have any questions or comments.
Re:Something about functions in Bash (2017) by adrianN on Friday July 06, 2018 @04:29AM (#17469848)
jq is pretty neat for dealing with JSON in a shell script.
Re:Something about functions in Bash (2017) by laumars on Friday July 06, 2018 @06:42AM (#17470329)
jq is a great tool but I wanted something that was more seamless than piping data to jq with a jq script quoted as a parameter. I wanted something where the shell itself could fragment and understand structured data (and not just JSON but YAML, TOML, HCL, CSV, XML, etc - though XML is proving more a great deal more troublesome than I'd hoped).

I'm not suggesting the work I'm doing is any better that jq though - there's a lot of areas where jq will run circles around my shell. I see it more as different design goals but with a fairly large area of overlap.

Re:Something about functions in Bash (2017) by vram22 on Friday July 06, 2018 @03:10PM (#17474265)
Re:Something about functions in Bash (2017) by laumars on Friday July 06, 2018 @07:07PM (#17475802)
Yeah. Personally I hate it. But it's what Terraform uses so sometimes it's handy having another tool to pull bits from Terraform scripts.
Re:Something about functions in Bash (2017) by chubot on Thursday July 05, 2018 @10:06PM (#17468390)
In OSH I use the technique of "lexer modes" to handle all the cases you mention:

http://www.oilshell.org/blog/2017/12/17.html

I have a few more blog posts on that topic I haven't published, but hopefully that gives the idea.

I probably used to agree with you (if I'm understanding correctly; it's not entirely clear what the conflict you see is). But after writing the parser in this style, I don't think it's too big a deal.

I plan to use the same technique to parse the Oil language, which is a new language without the legacy. The command syntax is largely the same, but compound constructs like function, if, while/for, etc. are different.

The parser is regularly tested on over a million lines of shell:

http://www.oilshell.org/blog/2017/11/10.html

Note that Python, JavaScript, and Swift all need something like this too, now that they have arbitrary code inside string literals, just like shell:

    print(f'three = {1 + 2}')  # Python f-strings
    
    console.log(`three = ${1 + 2}`)  # JavaScript

    println("three = \(1 + 2)")  # Swift