Using the result of a command as an argument in ba

2019-01-13 05:39发布

To create a playlist for all of the music in a folder, I am using the following command in bash:

ls > list.txt

I would like to use the result of the pwd (print working directory) command for the name of the playlist.

Something like:

ls > ${pwd}.txt

That doesn't work though - can anyone tell me what syntax I need to use to do something like this?

Edit: As mentioned in the comments pwd will end up giving an absolute path, so my playlist will end up being named .txt in some directory - d'oh! So I'll have to trim the path. Thanks for spotting that - I would probably have spent ages wondering where my files went!

7条回答
别忘想泡老子
2楼-- · 2019-01-13 05:59

to strip all but the directory name

ls >/playlistdir/${PWD##/*}.txt

this is probably not what you want because then you don't know where the files are (unless you change the ls command)

to replace "/" with "_"

ls >/playlistdir/${PWD//\//_}.txt

but then the playlist would look ugly and maybe not even fit in the selection window

So this will give you both a short readable name and usable paths inside the file

ext=.mp3 #leave blank for all files
for FILE in "$PWD/*$ext"; do echo "$FILE";done >/playlistdir/${PWD##/*}.txt
查看更多
登录 后发表回答