1

I've written a few dozen Bash scripts for my Mac over the years, although probably 80% of Bash code I have is in .bash_profile. Lately I've been doing things I used to do with Bash by using Python instead.

So, given languages like Python or Ruby (or even PHP), with the exception of login scripts such as .bash_profile (which may not be an exception), are there any tasks that Bash can do that generic scripting languages cannot?

Chuck
  • 4,662
  • 2
  • 33
  • 55
  • "Is there anything that an X script can accomplish that other scripting languages cannot?", for **any** value of X, is opinion-based flamebait. There are definitely things that are much easier to do *tersely* in bash, but once you get into discussion of which things it is and isn't valuable to have special syntax for your question is off in the land of dueling opinions. – Charles Duffy Oct 26 '17 at 19:59
  • Regarding the hold, I disagree that this is primarily opinion-based. I'm asking for a factual answer, not whether Bash is better or worse than a generic scripting language, but whether things are possible in Bash that aren't elsewhere. – Chuck Oct 26 '17 at 20:40
  • "Can something be done in language-X that can't be done in language-Y?" is categorically false for **every Turing-equivalent language**, unless you're asking if a language has hooks or facilities that go beyond computation and into side-effecting operations with the rest of the world or OS. – Charles Duffy Oct 26 '17 at 20:59
  • And the thing about side-effecting hooks and facilities is that (once again, categorically) any hook you can add for one language you can add for another, and/or add a translation layer for. If you have, say, a GUI library in Python/C/C++/TCL/whatever it's going to be possible -- even if impractical -- to implement the same library for any other language, even if it's doing something crazy inefficient like converting that higher-level API to a socket-based shim. – Charles Duffy Oct 26 '17 at 21:11
  • So if the question is read in a strict and limited enough matter to make it *not* flamebait, then it's meaningless in that its answer is guaranteed by principals introduced in any good CS101 course, and not specific to the individual language being asked about at all. – Charles Duffy Oct 26 '17 at 21:12

3 Answers3

2

Bash is oldschool UNIX - pulling little utilities together to achieve a greater goal mostly by using pipes and plumbing output from one command to the next.

There is definitely a lot to be said for having the skills involved in this style of seat of the pants programming. Too many people head off and write a self contained program to achieve something that can be done using a few command line inputs.

So in answer to your question , yes. A bash script can teach you to understand the multitude of bash scripts out there and it can do most things on a UNIX box in close to the most efficient way. Bash is here to stay.

Paula Livingstone
  • 1,175
  • 1
  • 12
  • 21
1

Well, first off, bash is itself a shell, so it comes with builtin features like job control (suspend, etc.), file handle/terminal redirection (2 &> 1 and friends) and terminal control (like being able to display the current path in the titlebar, etc.). Other languages that don't have a built-in shell with access to termcap don't have those abilities. Pipe redirection is hard to get right (python's subprocess.popen has a bunch of limitations due to threads and potential deadlocks for example, while bash has access to tee etc.)

cowbert
  • 3,212
  • 2
  • 25
  • 34
  • Absolutely everything bash can do (re: process substitution, command substitution, pipeline setup, &c) can be done with `subprocess.Popen`. Not necessarily *easily*, absolutely not tersely, but *can be done*. That includes use of `tee` (which is, after all, an external command and can be `exec`'d by a Python interpreter just as easily as it can be `exec`'d by bash). – Charles Duffy Oct 26 '17 at 20:02
  • sure, but any semi-robust solution requires wrangling threads, which is not trivial vs. a bash 1/2 liner. See https://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python – cowbert Oct 26 '17 at 20:11
  • Eh? The "non-blocking" requirement making that ticket complicated (and necessitating use of threads) isn't something bash supports or has any need of -- the bash-native way to do things is to fork off a subprocess (which does individually blocking operations), which Python is equally capable of doing. – Charles Duffy Oct 26 '17 at 20:15
  • That said, I fully and wholeheartedly agree (as someone who uses both tools extensively and regularly) that there are jobs where bash is a more appropriate tool than Python. However, that's (1) a very opinion-centric case-by-case determination, and (2) not, strictly, what the OP asked. – Charles Duffy Oct 26 '17 at 20:20
  • I posted the wrong one. Here's a couple of questions on how to implement `tee`: https://stackoverflow.com/questions/31833897/python-read-from-subprocess-stdout-and-stderr-separately-while-preserving-order https://stackoverflow.com/questions/31926470/run-command-and-get-its-stdout-stderr-separately-in-near-real-time-like-in-a-te – cowbert Oct 26 '17 at 20:20
  • "Implement" `tee`? If we were behaving as an analog of the equivalent bash, we'd just be using an OS-provided `/usr/bin/tee`, exactly as bash does. Which is to say, once again you're setting a higher bar for the Python than you do for the bash we're talking about replacing. – Charles Duffy Oct 26 '17 at 20:21
0

No. Bash is written in C, and the programs it runs are written in other languages (which are usually either C or implemented in C). Thus, everything that Bash does can be — and already is — done by other programming languages.

jwodder
  • 54,758
  • 12
  • 108
  • 124