본문 바로가기
Developer.log/Worklog

Worklog: Bash script vs. Python script - Why and which one to use

by taf 2023. 4. 18.

 



Recently I had a task in my current company to write a bash script to to delete unnecessary resources.
 
Since this was my first time writing the bash script, I searched for what are the benefits of using bash over python.


 
 

 Table of Contents

 

  • So why people use bash script?
  • Bash script vs. Python script
  • Basic grammars of writing scripts in bash

 
 
 

So why people use bash script?

 
There are multiple reasons but following are some of highlights that many developers use bash script:
 
 

• To automate repetitive tasks or processes, which can save time and reduce errors. ✅
 
• To simplify complex commands or sequences of commands that are difficult to remember or type out each time.
 
• To batch process files or data, such as renaming files, converting file formats, or processing text files.
 
• To run system maintenance or administrative tasks, such as backing up files, checking disk space, or monitoring system performance.
 
• To deploy and configure software applications, such as installing dependencies, setting environment variables, or configuring network settings.
 
• To interact with APIs or web services, such as sending HTTP requests or processing JSON data.
 
• To create custom command-line tools or utilities that perform specific tasks or integrate with other tools  
 
 
 
 

Bash script vs. Python script

 
 

Before I started writing bash script, I was used to writing scripts in python3.

 
 
So I was curious why other senior engineers asked me to write in bash script rather than python script. Of course, there is no definitive answer to whether Bash script or Python script is better as both have their own strengths and weaknesses.
 
 
However, I want to understand deeper about what difference between writing script in bash and Python. Here are some factors that may influence the choice of one over the other:
 

 
 

<Table of differences between bash vs Python script>

  Bash Python
Syntax Simple, but may be less readable for complex tasks More complex, but generally more readable
Libaries and Packages Limited built-in functionality, may require additional tools or programs Extensive standard library, plus many third-party packages available
Portability Pre-installed on most Unix-based systems, but may have compatibility issues across different systems Requires the installation of a Python interpreter, but can be used on multiple platforms
Speed Fast and efficient for simple tasks, but may be slower for complex tasks Generally slower for simple tasks, but can scale better for more complex tasks
Learning curve Easier to learn for beginners, but may have limitations for advanced tasks May require more time to learn, but can handle more complex tasks
Task types Well-suited for system administration, file handling, and text manipulation tasks Well-suited for data processing, scientific computing, and machine learning tasks
Debugging Limited debugging capabilities, may require the use of third-party tools Robust debugging capabilities, including built-in debugging tools

 
To be honest, as a person who already used to writing script in python, I felt writing bash script is much complex than wiritng in python. 
 
 
 
 

Basic grammars of writing script in bash

 

• The first line of the script should begin with a "shebang" (#!) and specify the path to the Bash interpreter (e.g. #!/bin/bash).
 
• Variables in Bash script are defined without any data type (e.g. my_var="hello world"), and are typically referenced using a dollar sign ($) before the variable name (e.g. echo $my_var).
 
• Bash script uses control structures such as if/else statements and loops (e.g. for loops, while loops) to control the flow of execution.
 
• Commands in Bash script are executed by typing the command name followed by any necessary arguments (e.g. echo "hello world").
 
• Bash script supports command substitution, which allows the output of a command to be used as an argument for another command (e.g. echo $(ls)).
 
• Bash script supports string manipulation, such as concatenation (e.g. $string1$string2), substring extraction (e.g. ${string:0:5}), and string substitution (e.g. ${string/foo/bar}).
 
• Bash script can read input from users or files using the read command and can write output to files using redirection (e.g. >, >>).
 
 


These are just some basic grammar rules, but there are many more advanced features and techniques that can be used in Bash script as well.

댓글