Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:06:52+00:00 2026-05-30T20:06:52+00:00

I have the following python function that allows me to run shell commands from

  • 0

I have the following python function that allows me to run shell commands from within a python script:

import subprocess   

def run_shell_command(cmd,cwd=None):
      retVal = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, cwd=cwd);
      retVal = retVal.stdout.read().strip('\n');
      return(retVal);

This allows me to do things like:

output = run_shell_command("echo 'Hello world'")

My question is: with the definition of run_shell_command above, which type of shell is started? (e.g. login vs interactive).

Knowing which shell is started would help know which which dot files (e.g. .bashrc, .profile, etc.) are executed prior to my shell command.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:06:53+00:00Added an answer on May 30, 2026 at 8:06 pm

    What shell is run?

    This is mentioned in the Python subprocess documentation:

    The executable argument specifies the program to execute. It is very seldom needed: Usually, the program to execute is defined by the args argument. If shell=True, the executable argument specifies which shell to use. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable. The only reason you would need to specify shell=True on Windows is where the command you wish to execute is actually built in to the shell, eg dir, copy. You don’t need shell=True to run a batch file, nor to run a console-based executable.

    /bin/sh on Linux/MacOSX is typically an alias for bash (or bash-compatible – newer versions of Debian use dash), whereas on Unixes like Solaris, it might be classic Bourne Shell.

    For Windows, it’s usually cmd or command.bat.

    Login shell or not via popen?

    I just realized that I haven’t answered your 2nd question – but setting shell=True will spawn a non-login shell (look @AndiDog’s source code link, the way the shell is getting forked would create a non-login shell).


    Security Implications

    Also be aware that using shell=True, while it allows you to use shell primitives and shortcuts, can also be a security risk, so be sure to check any possible inputs you might use for process spawning.

    Warning Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of shell=True is strongly discouraged in cases where the command string is constructed from external input:

    >>>
    >>> from subprocess import call
    >>> filename = input("What file would you like to display?\n")
    What file would you like to display?
    non_existent; rm -rf / #
    >>> call("cat " + filename, shell=True) # Uh-oh. This will end badly...
    

    shell=False disables all shell based features, but does not suffer from this vulnerability; see the Note in the Popen constructor documentation for helpful hints in getting shell=False to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have the following simple python test script that uses Suds to call a
I have the following DLL ('arrayprint.dll') function that I want to use in Python
I have the following python code using the twisted API. def function(self,filename): def results(result):
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
I have a function that looks like the following, with a whole lot of
I have a python program with a global function that is painful to test
I'm trying to understand if the following Python function: def factorial(i): if not hasattr(factorial,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.