[英]How do I run a program with a different working directory from current, from Linux shell?
[英]How to run a Linux shell command from a different directory without getting there?
如何從另一個目錄運行Linux shell命令而又沒有實際到達那里?
在下面,我想運行一個make
命令,但不進入源代碼目錄,即從我的主目錄:
me@mypc:~$ ~/my/source/code/directory/make #This is wrong!
我看過一些例子,建議如下:
me@mypc:~$ cd ~/my/source/code/directory; make
但這最終使我進入了該源代碼目錄,我想避免這樣做。
可能還有其他選擇:
me@mypc:~$ cd ~/my/source/code/directory; make; cd ~
但是在cee中變得復雜了。
我想知道是否有比這些更好,更簡單的方法?
你可以試試:
me@mypc:~$ (cd ~/my/source/code/directory; make)
括號告訴您的shell生成一個單獨的子shell,並帶有其自己的當前目錄,然后在該子shell中運行命令。 命令完成后,將關閉子外殼,然后返回主外殼,該主外殼的當前目錄從未更改。
在子外殼中執行,例如
(cd ~/my/source/code/directory; make)
或者,您可以使用make的-C
選項,例如
make -C ~/my/source/code/directory
您也可以使用
pushd ~/my/source/code/directory; make; popd
要么
current=`pwd`; cd ~/my/source/code/directory; make; cd "$current"
$ (cd directory && make)
您需要使用&&
而不是;
。 考慮類似
cd junk && rm -rf *
使用&&
,如果目錄junk
目錄不存在,bash將中止。 如果您嘗試cd junk; rm -rf *
cd junk; rm -rf *
和junk
cd junk; rm -rf *
不存在,您將刪除當前目錄中的所有內容:(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.