首屆「ARC WORLD TOUR 2020」因新型冠狀病毒疫情中止   《Fall Guys》第2賽季為線上競技場帶來全新回合和中世紀亂鬥   伊織萌代言電競家具品牌Bauhutte推出電競棉被 「棉花坦克BHB-1600」   收錄大量經典桌遊的《世界遊戲大全51》決定發布免費版!   SEGA「人中之龍7 光與闇的去向」限時優惠特賣中!   勇者鬥惡龍主題樂園「DRAGON QUEST ISLAND 大魔王索瑪與起始之島」將於2021年春季開幕!   萊莎2公開在即 Cosplayer伊織萌現身萊莎的鍊金工房 1比1 Figure 披露會!   本應該已經準備上架的超級話題大作《電馭叛客2077》再次發表延期消息 

Bash 執行「字串命令」的方法

Linux 的 Bash shell 裡面,若有一個字串需要執行,可以怎麼做呢?

Bash 執行「字串命令」的方法

Bash shell 要將此字串當 Shell 命令執行,可以有下述兩種作法:

  1. eval
  2. bash -c
    • If the -c option is present, then commands are read from the first non-option argument command_string.
    • If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.
    • The assignment to $0 sets the name of the shell, which is used in warning and error messages.

分別測試作法:(執行結果都一樣)

  1. eval “cd /; ls” # 執行完,帳號會在 / 的路徑上
  2. bash -c “cd /; ls” # 執行完,帳號還是在原始位置
  3. source

相關文章