《Disney +》 迪士尼、Marvel、彼思、星球大戰…  【試玩】終於上市了!在PS5上試玩「尼爾:人工生命 ver.1.22474487139…」!   走路、成長、採收!《Pikmin Bloom》遊戲玩法介紹影片大公開!   《決勝時刻:先鋒》將於11月5日上市   「GUILTY GEAR -STRIVE-」公測即將舉行!   《NieR Re[in]carnation》正式上線日確定!並與《NieR:Automata》聯名!   「魔物獵人 特別節目 2021.5.26」確定播出!更新情報與最新短片!   第27回TETRIS王者盃「瑪利歐派對 超級巨星合作祭!」舉辦決定!   新《Resident Evil Village》體驗版於4月18日登上PS4和PS5 

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

隨機商業新聞