《Disney +》 迪士尼、Marvel、彼思、星球大戰…  《Ghost of Tsushima》即將改編電影   《Five Nights at Freddy’s: Security Breach》首支實機遊玩影片公開   享受最高級的香味!濱田酒造 x 卡樂B 「飲酒時的最佳配搭組合」發售中!   《Stranger Of Paradise Final Fantasy Origin》試玩版預覽——正統派動作,遊戲世界觀令人不禁想起原作!   收錄14部食鬼名作的PAC-MAN MUSEUM+ 2022年發售!   主播宇內梨沙與狩野英孝同場直播「黎明死線」遊戲實況!   獨家預覽《Fall Guys》第6季新回合「Pipe Dream」   今天想來哪一道?日本PlayStation以料理形式介紹遊戲陣容! 

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+