# Bash in Python made easy

**URL:** <https://openplanetary.discourse.group/t/bash-in-python-made-easy/52>\
**Category:** Python\
**Created:** [May 9, 2019, 3:07pm UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52 "2019-05-09T15:07:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew.annex](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/andrew.annex/32/20_2.png) [@andrew.annex](https://openplanetary.discourse.group/u/andrew.annex)\
**Post date:** [May 9, 2019, 3:07pm UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/1 "2019-05-09T15:07:15Z")

</div>

When I need to run bash commands or isis3 programs within a python script I turn to a neat library called [sh](https://amoffat.github.io/sh/) to make my life easier and to avoid directly using the subprocess module myself. Go check it out, I also found a few other similar projects that exist and will list them below with sh.

[https://plumbum.readthedocs.io/en/latest/index.html](https://plumbum.readthedocs.io/en/latest/index.html)

> **[GitHub - amitt001/delegator.py: Subprocesses for Humans 2.0.](https://github.com/amitt001/delegator.py)**
>
> Subprocesses for Humans 2.0. Contribute to amitt001/delegator.py development by creating an account on GitHub.

[https://amoffat.github.io/sh/](https://amoffat.github.io/sh/)

---

<div class="post-metadata">

**Author:** ![mariodamore](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/mariodamore/32/13_2.png) [@mariodamore](https://openplanetary.discourse.group/u/mariodamore)\
**Post date:** [May 10, 2019, 10:53am UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/2 "2019-05-10T10:53:39Z")

</div>

I find also _sh_ incredibly useful. The standard option _subprocess_ isn’t bad, but need a lot more boilerplate to work with and less intuitive.  
Should we add some basic example comparison between the proposed solution?  
Something on the line “how can I list all the file in current directory with ls -l” ?

---

<div class="post-metadata">

**Author:** ![andrew.annex](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/andrew.annex/32/20_2.png) [@andrew.annex](https://openplanetary.discourse.group/u/andrew.annex)\
**Post date:** [May 10, 2019, 7:38pm UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/3 "2019-05-10T19:38:56Z")

</div>

Yeah we could, I provided a link to the documentation that has simple examples like that but it depends on the format intended for the forum, alternatively a medium post could be made

---

<div class="post-metadata">

**Author:** ![mariodamore](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/mariodamore/32/13_2.png) [@mariodamore](https://openplanetary.discourse.group/u/mariodamore)\
**Post date:** [May 11, 2019, 7:58am UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/4 "2019-05-11T07:58:30Z")

</div>

I thought something _a lá_ stackoverflow, with only a minimal working example, could be helpful for the reader looking for a taste of your suggestion. More that than would be hard to maintain.

---

<div class="post-metadata">

**Author:** ![michaelaye](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/michaelaye/32/11_2.png) [@michaelaye](https://openplanetary.discourse.group/u/michaelaye)\
**Post date:** [May 14, 2019, 6:22pm UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/5 "2019-05-14T18:22:40Z")

</div>

Is there maybe a better example? Because `ls -l` really should be done with `pathlib.Path('.').glob('*')`; I know, it looks more work, but is actually safer for directories with several thousand entries, where `ls` will fail, but `pathlib` (based on `os`) doesn’t.

---

<div class="post-metadata">

**Author:** ![mariodamore](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/mariodamore/32/13_2.png) [@mariodamore](https://openplanetary.discourse.group/u/mariodamore)\
**Post date:** [May 16, 2019, 6:51am UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/6 "2019-05-16T06:51:22Z")

</div>

Thousand times YES for pathlib, I love it!! Mainly because I was using sys+os before 🤮

The point is to show a command simple enough to not get new user lost in understanding what the command should do in the first place and focus on the python+shell.  
_ls_ is well known and has a rich set of subcommand.

Do you have any other suggestion ? Maybe something that accept variable and not only options would be nice.

My idea is to have one command and list how you can use it in python with different solution, than we can always point the user to that page. Maybe a post here / wiki in github would / post on medium (that I hate more and more) would be useful.

---

<div class="post-metadata">

**Author:** ![michaelaye](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/michaelaye/32/11_2.png) [@michaelaye](https://openplanetary.discourse.group/u/michaelaye)\
**Post date:** [May 16, 2019, 7:17am UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/7 "2019-05-16T07:17:01Z")

</div>

How about “grep -l string” to get the list of files that contain “string”?

---

<div class="post-metadata">

**Author:** ![mariodamore](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/mariodamore/32/13_2.png) [@mariodamore](https://openplanetary.discourse.group/u/mariodamore)\
**Post date:** [May 16, 2019, 8:37am UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/8 "2019-05-16T08:37:24Z")

</div>

Nice one, grep is good , something like `grep -f FILE -A 2 -B 2 -v -l string`

This tool is super nice to explain shell commands [explainshell.com - grep -f FILE -A 2 -B 2 -v -l string](https://explainshell.com/explain?cmd=grep+-f+FILE+-A+2+-B+2+-v+-l+string)

---

<div class="post-metadata">

**Author:** ![michaelaye](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/michaelaye/32/11_2.png) [@michaelaye](https://openplanetary.discourse.group/u/michaelaye)\
**Post date:** [May 21, 2019, 4:27pm UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/9 "2019-05-21T16:27:24Z")

</div>

Woah, how cool is that! Nice find!

---

<div class="post-metadata">

**Author:** ![mariodamore](https://yyz2.discourse-cdn.com/free1/user_avatar/openplanetary.discourse.group/mariodamore/32/13_2.png) [@mariodamore](https://openplanetary.discourse.group/u/mariodamore)\
**Post date:** [May 22, 2019, 8:05am UTC](https://openplanetary.discourse.group/t/bash-in-python-made-easy/52/10 "2019-05-22T08:05:54Z")

</div>

Yes, this is also super nice to explain to people your command in presentation for example.
