ksh storing result of a command to a variable

后端 未结 5 889
清酒与你
清酒与你 2021-01-13 15:55

I want to store the result of a command to a variable in my shell script. I cant seem to get it to work. I want the most recently dated file in the directory.



        
5条回答
  •  遥遥无期
    2021-01-13 16:23

    You need both quotes to ensure you keep the name even if it contains spaces, and also in case you later want more than 1 file, and "$(..)" to run commands in background

    I believe you also need the '-1' option to ls, otherwise you could have several names per lines (you only keep 1 line, but it could be several files)

    PRODUCT="$(ls -1t /some/dir/file* | head -1 | xargs -n1 basename)"
    

    Please do not put space around the "=" variable assignments (as I saw on other solutions here) , as it's not very compatible as well.

提交回复
热议问题