skip to Main Content

I’m trying to use VSCode cli code in a while loop in a Bash script to open a number of files, the names of which I’m reading from a text file.

When I run the code below, it opens a file correctly for the first line from the file I’m reading, and then it opens a file like code-stdin-XXX that is a list of the rest of the lines from filenames.txt. If I change code to echo in the script this runs fine, so I assume I’m missing something in the code cli options.

while read -r line ; do
    code - "../songs/$line.xhtml"
done < filenames.txt

How I can correctly get code to open the file for each line of filenames.txt?

2

Answers


  1. for line in $(cat filenames.txt); do
        code "../songs/$line.xhtml"
    done
    
    Login or Signup to reply.
  2. This is all you need:

    xargs code -- < filenames.txt
    
    • xargs: Reads file names from STDIN (filenames.txt) and pass them as argument to the command.
    • code --: Executes code with options stopper -- and xargs will append filenames as arguments.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search