Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strengths of Shell Scripting compared to Python [closed]

Tags:

python

shell

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.

like image 266
Amol Gawai Avatar asked Apr 28 '09 05:04

Amol Gawai


People also ask

Which is better shell scripting or Python?

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.

What are the advantages of shell scripting?

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.

What is one of the advantages of shell scripts over compiled languages?

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.

Is Python shell scripting?

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.


2 Answers

  • Shell scripting has simpler notations for I/O redirection.
  • It is simpler to create pipelines out of existing programs in shell.
  • Shell scripting reuses entire programs.
  • Shell is universally available (on anything like Unix) - Python is not necessarily installed.

'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.

like image 59
Jonathan Leffler Avatar answered Sep 20 '22 12:09

Jonathan Leffler


"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.

  1. 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 <).

  2. 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.

like image 24
S.Lott Avatar answered Sep 22 '22 12:09

S.Lott