Bash Vs Python

by H. Fosdick   © 2024 RexxInfo.org


For those who know one of these languages and want to learn about the other. Click here for PDF version.


The Basics


 BashPython
   
Easy to learn, use, and maintain  No  (unfriendly syntax)  Yes
Very powerful  Yes  Yes
Open source  Yes  Yes
Portable  Yes  Yes
Runs on all platforms  Yes  Yes
Runs as the OS Shell  Yes  No
Interfaces to tons of tools  Yes  Yes
ANSI or ISO Standard  Yes
 (POSIX compliant with extensions)
  No
 (de facto standard by PEPs)

Profiles


 BashPython
   
DialectsBash (a superset of the Bourne shell)Version 2.x, Version 3.x, specialized implementations
Unique Usage * Default scripting language for Linux (including on Windows and mainframe Linux systems)
* Sometimes the default for BSD, Oracle Solaris, older Apples, other systems
* Most popular programming language in the world
* Most widely taught language
* Ships with many OS's
Programming paradigmsProcedural, functional, scriptingProcedural, functional, scripting, object-oriented
OOP: classes, objects, multi-inheritance, polymorphism, encapsulationUnsupportedYes
User GroupFree Software Foundation Python community
Quick Online LookupDevHints, LinuxTutorials QuickRef
Cheat Sheet
(printable PDF)
LinuxSimplify, Cheatography Python 3, Cheatography
ForumLinuxQuestions.org, Linux.org Python forums
Further informationFree Software Foundation: GNU Bash Python.org


Language Comparison


 BashPython
   
FormattingFree formBased on indentation rules
Case-sensitiveYesYes
CommentsStart comment with: #  Or, use a Here document for commenting out multiple linesStart line with # or enclose inside triple quotes (""")
Line Continuation\ (backslash)\ (backslash) or implicit
Statement Separator; (semi-colon); (semi-colon)
Code BlocksDefine by do - done, if - fi, or case - esacDefine by : (colon) plus indentation
Undefined VariablesAllowed. To error on undefined variables, code:  set -uUndefined variable raises NameError. Circumvent this by assigning special constant None.
Assignment Operators=  +=  -=  *=  /=  %=
Compound Bitwise:  &=  |=  ^=  <<=  >>=
=  +=  -=  *=  /=  %=  //=  **=  :=
Compound Bitwise:  &=  |=  ^=  >>=  <<=
Arithmetic Operators+  -  *  /  %  **
Compound:  +=  -=  *=  /=  %=
Evaluate arithmetic expression:
$((expression))
Increment/decrement a value:
i++  i--  ++i  --i
+  -  *  /  %  **  //
Compound: +=  -=  *=  /=  %=  //=  **=
Comparison OperatorsIntegers:  -eq  -ne  -gt  -ge  -lt  -le
Strings:  =  ==  !=  >  >=  <  <=  =~
==  !=  >  <  >=  <=  
Object Identity: is  is not
Logical Operators&&  ||  ! (prefix) and  or  not
Concatenation Operators+=  Or, use abuttal: VAR3="$VAR1$VAR2" +   +=   * (multiple copies of a string)    Concatenate literal strings with blank beween. Use f-strings. Use join or format methods
Bitwise Operators&  |  ^  <<  >>  ~
Compound:  &=  |=  ^=  <<=  >>=
&  |  ^  <<  >>  ~
Compound: &=  |=  ^=  <<=  >>=
Membership OperatorsUnsupported in  not in
Regular ExpressionsYes Use the re module
Built-in FunctionsNone in the traditional sense, designed to issue shell and line commands About 70 functions
Data TypesBy default, variables are untyped. Or use "declare" to explicitly type them as: -a, -A, -i, -l, -n, -r, -t, -u, -xA dynamically typed language with these data types: text (str), numeric (int, float, complex), sequence (list, tuple, range), mapping (dict), set (set, frozenset), boolean (bool), binary (bytes, bytearray, memoryview)
Function to Check Data Typedeclare -ptype
Collections of VariablesOne-dimensional arrays:
declare -a array_name
list, tuple, range, set, frozenset, dict, bytearray
Associative Arraysdeclare -A array_nameUse a dictionary (dict)
Multidimensional ArraysUnsupportedUse NumPy library
Stack & Queue OperationsNo generalized facility (offers a directory stack with pushd, popd, and dirs)Yes (queue module and deque from collections)
Decimal ArithmeticInstall and use: bcUse the decimal module
Flow of Controluntil, while, for, break, continue, return, exit if, for, while, break, continue, pass, return, exit, quit, match-case
Trace Script ExecutionUse the -x option.
Run the script: bash -x
or inside the script: set -x
Use the trace or pdb modules
Terminate Processexitexit()
Get User Inputecho -n "Enter your name: "
read name
name = input("Enter your name: ")
Exception Handlingtraptry-except-else-finally, raise, with
Standard Exceptionstrap covers 64 signal specs
(list them by: trap -l)
SyntaxError, TypeError, NameError, IndexError, KeyError, ValueError, AttributeError, IOError, ZeroDivisionError, ImportError
Run an Operating System CommandJust issue the command stringUse the subprocess module (or older os module)

Based on Bash Reference Manual version 5.2., Python Tutorial, and Python Language Reference (v 3.12.3).


Back to RexxInfo.org    .     .