In my bash function, I am using grep to capture a matching pattern from a string (selecting the files created), and then storing what is captured in an array. The string is assigned to a variable called output
installing route
create app/routes/foo.js
create app/templates/foo.hbs
updating router
add route foo
installing route-test
create tests/unit/routes/foo-test.js
after running
files=($(echo "$output" | ggrep -oP 'create\s\K(.+)'))
I confirm that I am capturing what I intend to by running echo ${files[*]}
. Output in the terminal looks as so
app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js
My goal is to pass these files as arguments to an npm script (npm run lint <list of files>
). However, when I try to plug in my variable to the npm script execution, the file names either print out like
"app/routes/foo.js" "app/templates/foo.hbs" "tests/unit/routes/foo-test.js"
or
"app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js"
The ultimate goal here is to be able to run
npm run lint app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js
I have tried tons of combinations to interpolate my file
variable, but nothing seems to work. I feel like I am missing something conceptually or approaching this in the wrong way. I originally was trying this without using an array for file
, but I was running into the same issue. My first pass is getting this to run in bash. However, I'd prefer to stick with using grep
or sed
here though. Anyone have any suggestions?
After reading suggestions I have tried
parsedfiles=$(echo "${files[*]}" | sed -e 's/"//g')
echo $parsedfiles
npm run lint $parsedfiles
When echo $parsedfiles
runs my terminal output looks good app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js
, but on the next line, it still outputs as npm run "app/routes/foo.js" "app/templates/foo.hbs" "tests/unit/routes/foo-test.js"
SOLVED
The real issue here wasn't about how I was passing the args in. It's an issue with actually running the npm script
. When passing args to the npm script it automatically puts them in double quotes.