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 8109433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:25:04+00:00 2026-06-06T01:25:04+00:00

How can I execute a shell command, can be complicated like normal command in

  • 0

How can I execute a shell command, can be complicated like normal command in bash command line, get the output of that command and pwd after execution?

I used function like this:

import subprocess as sub

def execv(command, path):
    p = sub.Popen(['/bin/bash', '-c', command],
                    stdout=sub.PIPE, stderr=sub.STDOUT, cwd=path)
    return p.stdout.read()[:-1]

And I check if user use cd command but that will not work when user use symlink to cd or other wierd way to change directory.

and I need a dictionary that hold {'cwd': '<NEW PATH>', 'result': '<COMMAND OUTPUT>'}

  • 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-06-06T01:25:05+00:00Added an answer on June 6, 2026 at 1:25 am

    I redirect stdout to stderr of pwd command. if stdout is empty and stderr is not a path then stderr is error of the command

    import subprocess as sub
    
    def execv(command, path):
        command = 'cd %s && %s && pwd 1>&2' % (path, command)
        proc = sub.Popen(['/bin/bash', '-c', command],
                         stdout=sub.PIPE, stderr=sub.PIPE)
        stderr = proc.stderr.read()[:-1]
        stdout = proc.stdout.read()[:-1]
        if stdout == '' and not os.path.exists(stderr):
            raise Exception(stderr)
        return {
            "cwd": stderr,
            "stdout": stdout
        }
    

    UPDATE: here is better implemention (using last line for pwd and don’t use stderr)

    def execv(command, path):
        command = 'cd %s && %s 2>&1;pwd' % (path, command)
        proc = sub.Popen(['/bin/bash', '-c', command],
                         env={'TERM':'linux'},
                         stdout=sub.PIPE)
        stdout = proc.stdout.read()
        if len(stdout) > 1 and stdout[-1] == '\n':
            stdout = stdout[:-1]
        lines = stdout.split('\n')
        cwd = lines[-1]
        stdout = '\n'.join(lines[:-1])
        return {
            "cwd": cwd,
            "stdout": man_to_ansi(stdout)
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby, I know I can execute a shell command with backticks like so:
I can thought that it will open a shell, execute the parameter (shell command)
On the Windows command line and cygwin bash I can execute the following without
How can I execute an SQL command through a shell script so that I
How can i execute a shell (bash) command within a Common Lisp program and
How can I execute a Java System (shell) command that has a space in
I'm looking to execute a shell command in Go and get the resulting output
how can I execute a shell command in the background from within a bash
Trying to execute shell command in background using pythons commands module >>>import commands >>>output
how can I make sure that Apache/web server user is allowed to execute shell

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.