如何在大括號擴展之前執行命令替換?

如何在大括號擴展之前執行命令替換?

我想在大括號擴展之前執行命令替換,但不能:

$ ls {$(seq -s , 13 20)}.pdf
ls: cannot access {13,14,15,16,17,18,19,20}.pdf: No such file or directory

我該怎麼做?

答案1

您只需要使用eval內建的 shell:

$ eval ls {$(seq -s , 13 20)}.pdf

eval傳遞給它的參數在哪裡:

ls {$(seq -s , 13 20)}.pdf

並將它們連接成一個命令:

ls {13,14,15,16,17,18,19,20}.pdf

然後由 shell 讀取並執行。

$ eval ls {$(seq -s , 13 20)}.pdf
13.pdf  14.pdf  15.pdf  16.pdf  17.pdf  18.pdf  19.pdf  20.pdf

答案2

你有沒有嘗試過

ls $(seq -f %.0f.pdf 13 20 )
  • -f給出格式字串
  • .0f對於 0 位小數浮點數
  • .pdf明顯的

(並且您了解 $( ) 語法)

相關內容