《Disney +》 迪士尼、Marvel、彼思、星球大戰…  「Nintendo Switch Online + 擴充包」詳情發表!   能夠預覽自己試戴Razer電競口罩「Project Hazel」的AR濾鏡登場!   疫情下的東京電玩展2021現場直擊!   期待強作齊聚一堂!「State of Play 2021430」發表內容彙整!   區塊鏈遊戲「The Sandbox」中的土地「 LAND」已經在可以輕鬆買賣NFT的平台「 Coincheck NFT(β版)」上販售!   《Final Fantasy VII Remake Intergrade》:7大提示和訣竅教你得心應手扮演尤菲‧如月   【速報】系列最新作《勇者鬥惡龍XII》正式發表!   《秘境探險》(Uncharted) 電影預告片全球首播 

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

隨機商業新聞

Disney Plus