《Disney +》 迪士尼、Marvel、彼思、星球大戰…  手機版 「Pokémon UNITE」 終於正式推出 ! 完成登入任務可領取特別服裝 !   《魔物獵人崛起》免費大型更新「Ver.3.0」發布!追加新魔物&新結局!   有機會免費得到PS5!?PS5 × ZONe限定聯名罐日本發售!   《Destruction AllStars》下一波精彩內容: 指定賽事、挑戰系列賽等更新   三月份PlayStation Plus遊戲:《Final Fantasy VII Remake》、《Maquette》、《Knack 2》以及《Farpoint》   電競毛毯與電競椅等特價發售!Bauhutte的2022年新年首賣預告!   Nintendo Switch版「十三機兵防衛圏」決定販售!   「薩爾達無双 厄災之黙示錄」特價發售! 

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 <(echo "cd /; ls") # 感謝 typebrook 提供的作法
Tsung

隨機商業新聞

Micorsoft