I tried to learn shell(bash) scripting few times but was driven away by the syntax. Then I found Python and was able to do most of the things a shell script can do in Python. I am now not sure whether I should invest my time in learning shell scripting anymore. So I want to ask:
What are strengths of shell scripting that make it an indispensable tool as compared to Python?
I am not a system administration by profession, but I am interested in setting up Linux systems for home users, hence I think learning shell scripting can become necessary.
Python is the most elegant scripting language, even more than Ruby and Perl. On the other hand, Bash shell programming is actually very excellent in piping out the output of one command into another. Shell Scripting is simple, and it's not as powerful as python.
The many advantages include easy program or file selection, quick start, and interactive debugging. A shell script can be used to provide a sequencing and decision-making linkage around existing programs, and for moderately sized scripts the absence of a compilation step is an advantage.
Shell scripting is meant to be simple and efficient. It uses the same syntax in the script as it would on the shell command line, removing any interpretation issues. Writing code for a shell script is also faster and requires less of learning curve than other programming languages.
Python provides methods to run shell commands, giving us the same functionality of those shells scripts. Learning how to run shell commands in Python opens the door for us to automate computer tasks in a structured and scalable way.
'Tis true that you can do everything in Python that you can do in shell; 'tis also true that there are things that are easy in Python that are hard in shell (just as there are things that are easy in shell but hard in Python). Knowing both will be best in the long term.
"What are strengths of shell scripting that make it an indispensable tool as compared to Python?"
The shell is not indispensable. Why do you think there are so many? bash, tcsh, csh, sh, etc., etc.,
Python is a shell. Not the one you'd use for running all commands, but for scripting, it's ideal.
Python is a more-or-less standard part of all Linux distro's.
The more traditional shells do too many things.
They have a handy user interface for running commands. This includes one-line commands where the shell searches your PATH, forks and execs the requested program. It also includes pipelines, sequences and concurrent programs (using ;
, |
and &
) as well as some redirection (using >
and <
).
They have a crummy little programming-language-like capability for running scripts. This language is rather hard to use and extremely inefficient. Most statements in this language require forking one or more additional processes, wasting time and memory.
Running programs from the shell, redirecting stderr to a log file and that kind of thing is good. Do that in the shell.
Almost everything else can be done more efficiently and more clearly as a Python script.
You need both. However, you should never write a script with if-statements or loops in a traditional shell language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With