• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Modules In Python / How to use sh in Python

How to use sh in Python

Author: PFB Staff Writer
Last Updated: August 27, 2020

What is sh?

sh is a unique subprocess wrapper that maps your system programs to Python
functions dynamically. sh helps you write shell scripts in Python by giving you
the good features of Bash (easy command calling, easy piping) with all the power
and flexibility of Python. [source]

Starting with sh

sh is a full-fledged subprocess interface for Python that allows you to call any
program as if it were a function. sh lets you call just about anything that you
could run from a login shell much more neatly than you can with subprocess.Popen,
and more importantly lets you capture and parse the output much more readily.

Installation

The installation of sh is done through the pip command

pip install sh

Usage

The easiest way to get up and running is to import sh directly or import your
program from sh. Every command you want to run is imported like any other module.

That command is then usable just like a Python statement.

Arguments are passed per usual, and output can be captured and worked with in a
like fashion.

# get interface information
import sh
print sh.ifconfig("eth0")

from sh import ifconfig
print ifconfig("eth0")

# print the contents of this directory
print ls("-l")

# substitute the dash for an underscore for commands that have dashes in their names
sh.google_chrome("http://google.com”)

Executing Commands with sh

Commands are called just like functions.

“Note that these aren’t Python functions, these are running the binary commands
on your system dynamically by resolving your PATH, much like Bash does. In this
way, all the programs on your system are easily available in Python.”

Many programs have their own command subsets, like git (branch, checkout).

sh handles subcommands through attribute access.

from sh import git

# resolves to "git branch -v"
print(git.branch("-v"))

print(git("branch", "-v")) # the same command

Keyword Arguments

Keyword arguments also work like you’d expect: they get replaced with the
long-form and short-form commandline option. [source]

# Resolves to "curl http://duckduckgo.com/ -o page.html --silent"
sh.curl("http://duckduckgo.com/", o="page.html", silent=True)

# If you prefer not to use keyword arguments, this does the same thing
sh.curl("http://duckduckgo.com/", "-o", "page.html", "--silent")

# Resolves to "adduser amoffat --system --shell=/bin/bash --no-create-home"
sh.adduser("amoffat", system=True, shell="/bin/bash", no_create_home=True)

# or
sh.adduser("amoffat", "--system", "--shell", "/bin/bash", "--no-create-home”)

Finding Commands

“Which” finds the full path of a program, or returns None if it doesn’t exist.

This command is one of the few commands implemented as a Python function, and
therefore doesn’t rely on the “which” program actually existing.

print sh.which("python")     # "/usr/bin/python"
print sh.which("ls")         # "/bin/ls"

if not sh.which("supervisorctl"): sh.apt_get("install", "supervisor", “-y”)

There are many more features that you can use with sh, and you can find them all
in the official documentation.

Baking

sh is capable of “baking” arguments into commands.

# The idea here is that now every call to ls will have the “-la” arguments already specified.
from sh import ls

ls = ls.bake("-la")
print(ls) # "/usr/bin/ls -la"

# resolves to "ls -la /"
print(ls(“/“))
Baked ssh command

Calling “bake” on a command creates a callable object that automatically passes
along all of the arguments passed into “bake”.

# Without baking, calling uptime on a server would be a lot to type out:
serverX = ssh("myserver.com", "-p 1393", "whoami")

# To bake the common parameters into the ssh command
myserver = sh.ssh.bake("myserver.com", p=1393)

print(myserver) # "/usr/bin/ssh myserver.com -p 1393”

Now that the “myserver” callable represents a baked ssh command, you can call
anything on the server easily:

# resolves to "/usr/bin/ssh myserver.com -p 1393 tail /var/log/dumb_daemon.log -n 100"
print(myserver.tail("/var/log/dumb_daemon.log", n=100))

# check the uptime
print myserver.uptime()
 15:09:03 up 61 days, 22:56,  0 users,  load average: 0.12, 0.13, 0.05

For more advanced features, please see the official documentation.

Sources

https://github.com/amoffat/sh
https://github.com/Byzantium/Byzantium

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Modules In Python, sh Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us