Linux下使用Shell指令碼改變當前工作路徑

來源:互聯網
上載者:User

 

Shell 指令碼執行有三種方法

bash 指令碼名

sh 指令碼名

chmod +x 指令碼名

使用下面語句測試

#a.sh(副檔名可不要)cd /pwdecho "complete"

然而具體執行時候,發現過程中改變過路徑,然而最終結果是並沒有改變路徑。

[fedora@localhost ~]$ bash a.sh/complete[fedora@localhost ~]$

原因分析:

執行指令碼時候,只是在當前的shell下開了一個子進程,切換目錄的操作只對該進程中相關後續指令有效,但改變不了父進程的目錄。

解決方案:

法一:

用 source a.sh就行了。

法二:

[fedora@localhost ~]$ cd ~[fedora@localhost ~]$ chmod u+x a.sh[fedora@localhost ~]$ . ./a.sh /complete[fedora@localhost /]$

對於 $ . ./a.sh

第一個點是bash的內部命令,表示在當前shell中運行

後面的“./a.sh”是命令的參數,即要執行的指令碼

註:兩點之間要有空格。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.