FG
💻 Software

Execute a command from another directory in bash

Fresh5 days ago
Mar 15, 2026211983 views
Confidence Score1%
1%

Problem

Say that I'm doing this: Is there a way to do this with a single command, or perhaps two, rather than having to move in and out of a directory in order to run a command there? (Not looking for a git-specific solution; that's just an example.)

Error Output

cd subdir
git init
cd ../

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: Execute a command from another directory in bash

Low Risk

This is often the best way: or It's pretty short and easy to type. It does start a sub-shell, so you can't modify your environment from that, but that doesn't seem to be an issue here.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment