There are several good reasons why you should learn Bash (or another shell e.g., Zsh). The greatest advantages comes in terms of efficiency and flexibility.
At the book Automate the Boring Stuff’s web page the introduction starts with:
If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?
When it comes to automating the boring stuff you can do a lot with shell scripting and in certain situations it can be stronger than e.g. Python (and sometimes much weaker).
The shell is good when it comes to piping, redirection, stdin, stdout, stderr, etc. Since Bash is design to talk to the OS there are several fantastic features that makes your life easier. I recently had to automate some scripting on a server. Data was pushed to the server and needed resampling before entering a database. I had Python scripts that resampled the data, but I wanted it to happen not once but continuously as data streamed in. Here Bash provided some nice ways to link the scripts together and set it up with Crontab (job scheduler).
Bash is clearly good at automating things that has to do with the OS. But Python has modules such as glob and os that can handle much of the same issues. So when to use Bash and when to use e.g. Python? I guess it boils down to taste and the task at hand.
Handling data in Bash is not optimal.
Bash is excellent at small scripts that chain existing shell command together.
Python is a lot easier to maintain than Bash, especially large Bash scripts can get really messy.
“As soon as I’m doing anything more complex than a for loop I switch to Python.”
“My ~/bin folder is full of little useful bash scripts I’ve written. Some are very old and continue to work, upgrade after upgrade. (How much 15-year-old Python code still ‘just works’?)”
“I find that it’s easier to pre-process data using Bash + built-ins like sed, awk, tr and cut, rather than trying to do the same work in Python. Suppose that you have a 2GB text file with multiple columns and you want just a few of them that have certain values in them. It’s pretty simple and fast do that extraction before loading the whole thing into Python (or worse, parsing line by line).”
“It really boils down to what is the best tool for the job.”
“Bash starts to suck after about 100 lines of code, and after 500 it starts to become virtually unmaintainable. Small ad hoc scripts have a habit of growing into bigger beasts as time goes on.”
“The Bash type system is basically ‘it’s a string, deal with it somehow’. This leads to all sorts of weird and unpredictable behavior where instead in Python you’d get a TypeError or ValueError that’s easy enough to track down.”
“Python has an ecosystem of tens of thousands of packages that do all sorts of things that you may want (e.g. reading CSV files or XML or whatever). Bash can just run programs that input files or text and output text.”
“My rule is if it takes under 5 minutes to write and I’m probably going to throw it away I do it in Bash. Anything else I use Python.”
“Bash’s single advantage is longevity. There are Bash scripts that are older than Python itself that are still running just fine.”
If you are new to Bash I’d recommend the book the Linux Command Line.
Also FullBashGuide is a good resource.
There are several programs that can be used in the CLI to learn more about the shell. Specifically useful programs are,
Command | Description |
---|---|
help | Reference page for shell builtin |
man | Display Commands manual page |
tldr | Simplified man pages |
A terminal emulator is a program that allows you to use the terminal with some more advanced features. On Windows I e.g., use cmder which looks more appealing and has features such as multiple tabs. A Unix alternative would e.g., be the GNOME Terminal or rxvt.
This article gives a good overview over terminal emulators. Also, here’s a list from Arch wiki.
Bash is the most common Unix shell language, it is as many other shells an extension of the Bourne shell. There are several other shells as well e.g., the fish shell (friendly interactive shell) which aims at providing a user friendly shell.
UNIX shell differences and how to change your shell (Monthly Posting)
Hyperpolyglot – Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh