Hacker News new | past | comments | ask | show | jobs | submit login

I have written a lot of bash. When you know what you're doing it's very productive. But it still feels like walking a tightrope, where some corner case in quoting, interpolation, comparison, etc will one day rm -rf / you.

What i really want is "python, but with really easy running of subcommands". Imagine extending python with a $ operator (prefix, applied to iterables) so that

  files_iter = $('ls')
would run ls and put an iterator over its lines of output in that variable, throwing an exception if ls exits with an error status (i realise there is a rabbithole of subtleties here - getting those right would be part of this). Or

  contains_pattern = $?('grep', '-q', pattern, file)
to get just the exit status as a boolean. I think i'd drop bash in a heartbeat.



Languages like Ruby, Perl, and PHP have a backtick that can be used for that; for example in Ruby:

    >> files = `ls / | grep ^b`.split("\n")
    => ["bin", "books", "boot"]
    >> p $?
    #<Process::Status: pid 10889 exit 0>

    >> `false`
    => ""
    >> p $?
    #<Process::Status: pid 10898 exit 1>
I don't use it a lot because I don't feel like installing Ruby just for a few scripts, and zsh solves enough of the problems for me anyway.

Also parsing ls output isn't necessarily a good idea, and what you really miss is "first class globbing" like:

  for f in /b*; [..]
  arr=(/b*)


Backticks are going in the right direction, but not quite it. I want to operate on lists (or iterables etc), not strings, so i have all the usual safe and convenient facilities of the language available to build them. I want to get an object back that is extremely easy to get various kinds of results out of (Python's subprocess object isn't; i'm not familiar with Ruby's). I want more aggressive raise_for_status style error checking.

Thinking about it, this doesn't need to be an operator, and i could probably just write this myself and start trying it.

Interesting point about globbing. My feeling is that i don't actually use it a lot in scripts - the criteria for matching are usually complicated enough that i'm much more likely to use find. In a project with 1376 non-comment lines of shell script, i found eleven uses of globbing.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: