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

<pre><code>ls &gt; list.txt
</code></pre>

<p>I would like to use the result of the <code>pwd</code> command for the name of the playlist.</p>

<p>Something like:</p>

<pre><code>ls &gt; ${pwd}.txt
</code></pre>

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

<p><strong>Edit:</strong> 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!</p>

[{'answer_id': 58212, 'author': 'John Calsbeek', 'author_id': 5696, 'author_profile': 'https://Stackoverflow.com/users/5696', 'pm_score': 8, 'selected': True, 'text': '<p>The best way to do this is with <code>"$(command substitution)"</code> (thanks, <a href="https://stackoverflow.com/a/58233/1899640">Landon</a>):</p>\n\n<pre><code>ls &gt; "$(pwd).txt"\n</code></pre>\n\n<p>You will sometimes also see people use the older backtick notation, but this has <a href="https://stackoverflow.com/questions/4708549/what-is-the-difference-between-command-and-command-in-shell-programming">several drawbacks</a> in terms of nesting and escaping:</p>\n\n<pre><code>ls &gt; "`pwd`.txt"\n</code></pre>\n\n<p>Note that the unprocessed substitution of <code>pwd</code> is an absolute path, so the above command creates a file with the same name in the same directory as the working directory, but with a <code>.txt</code> extension. Thomas Kammeyer pointed out that the <code>basename</code> command strips the leading directory, so this would create a text file in the current directory with the name of that directory:</p>\n\n<pre><code>ls &gt; "$(basename "$(pwd)").txt"\n</code></pre>\n\n<p>Also thanks to erichui for bringing up the problem of spaces in the path.</p>\n'}, {'answer_id': 58214, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 2, 'selected': False, 'text': "<p>The syntax is:</p>\n\n<pre><code>ls &gt; `pwd`.txt\n</code></pre>\n\n<p>That is the '`' character up underneath the '~', not the regular single quote.</p>\n"}, {'answer_id': 58224, 'author': 'Thomas Kammeyer', 'author_id': 4410, 'author_profile': 'https://Stackoverflow.com/users/4410', 'pm_score': 3, 'selected': False, 'text': '<p>To do literally what you said, you could try:</p>\n\n<pre><code>ls &gt; `pwd`.txt\n</code></pre>\n\n<p>which will use the full pathname, which should be fine.\nNote that if you do this in your home directory, which might\nbe in /home/hoboben, you will be trying the create /home/hoboben.txt,\na text file in the directory above.</p>\n\n<p>Is this what you wanted?</p>\n\n<p>If you wanted the directory to contain a file named after it, you would get\nthe basename of the current directory and append that with .txt to the pwd.</p>\n\n<p>Now, rather than use the pwd command... why not use the PWD environment variable?</p>\n\n<p>For example:</p>\n\n<pre><code>ls &gt; $PWD.txt\n</code></pre>\n\n<p>or</p>\n\n<pre><code>ls &gt; ${PWD}.txt\n</code></pre>\n\n<p>is probably what you were trying to remember with your second example.</p>\n\n<p>If you\'re in /home/hoboben and you want to create /home/hoboben/hoboben.txt, try:</p>\n\n<pre><code>ls &gt; ${PWD}/${PWD##*/}.txt\n</code></pre>\n\n<p>If you do this, the file will contain its own name, so most often, you would remedy this in one of a few ways.  You could redirect to somewhere else and move the file or name the file beginning with a dot to hide it from the ls command as long as you don\'t use the -a flag (and then optionally rename the resulting file).</p>\n\n<p>I write my own scripts to manage a directory hierarchy of music files and I use subdirectories named ".info", for example, to contain track data in some spare files (basically, I "hide" metadata this way).  It works out okay because my needs are simple and my collection small.</p>\n'}, {'answer_id': 58227, 'author': 'Mark Nold', 'author_id': 4134, 'author_profile': 'https://Stackoverflow.com/users/4134', 'pm_score': 2, 'selected': False, 'text': '<p>Using the above method will create the files one level above your current directory. If you want the play lists to all go to one directory you\'d need to do something like:</p>\n\n<pre><code>#!/bin/sh\n\nMYVAR=`pwd | sed "s|/|_|g"`\nls &gt; /playlistdir/$MYVAR-list.txt\n</code></pre>\n'}, {'answer_id': 58233, 'author': 'Landon', 'author_id': 1597, 'author_profile': 'https://Stackoverflow.com/users/1597', 'pm_score': 3, 'selected': False, 'text': '<p>This is equivalent to the backtick solution:</p>\n\n<pre><code>ls &gt; $(pwd).txt\n</code></pre>\n'}, {'answer_id': 58235, 'author': 'erichui', 'author_id': 6034, 'author_profile': 'https://Stackoverflow.com/users/6034', 'pm_score': 3, 'selected': False, 'text': '<p>I suspect the problem may be that there are spaces in one of the directory names.  For example, if your working directory is "/home/user/music/artist name".  Bash will be confused thinking that you are trying to redirect to /home/user/music/artist and name.txt.  You can fix this with double quotes</p>\n\n<pre><code>ls &gt; "$(pwd).txt"\n</code></pre>\n\n<p>Also, you may not want to redirect to $(pwd).txt.  In the example above, you would be redirecting the output to the file "/home/user/music/artist name.txt"</p>\n'}, {'answer_id': 9589723, 'author': 'technosaurus', 'author_id': 1162141, 'author_profile': 'https://Stackoverflow.com/users/1162141', 'pm_score': 0, 'selected': False, 'text': '<p>to strip all but the directory name</p>\n\n<pre><code>ls &gt;/playlistdir/${PWD##/*}.txt\n</code></pre>\n\n<p>this is probably not what you want because then you don\'t know where the files are (unless you change the ls command)</p>\n\n<p>to replace "/" with "_"</p>\n\n<pre><code>ls &gt;/playlistdir/${PWD//\\//_}.txt\n</code></pre>\n\n<p>but then the playlist would look ugly and maybe not even fit in the selection window</p>\n\n<p>So this will give you both a short readable name and usable paths inside the file</p>\n\n<pre><code>ext=.mp3 #leave blank for all files\nfor FILE in "$PWD/*$ext"; do echo "$FILE";done &gt;/playlistdir/${PWD##/*}.txt\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58207', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/840/']