Here are the details of my setup. I have the following files:
config.json
{
"paths": ["/Users/First Folder", "/Users/Second Folder"]
}
test.sh
#!/bin/zsh
monitored_paths=$(jq -r '.paths[]' config.json)
fswatch --verbose "${monitored_paths[@]}"
The paths key in the JSON array needs to be processed by jq and then expanded as arguments. However, when executing test.sh, I encounter this output:
Output:
start_monitor: Adding path: /Users/First Folder
/Users/Second Folder
My expectation is to have:
start_monitor: Adding path: /Users/First Folder
start_monitor: Adding path: /Users/Second Folder
In summary, I aim to monitor two files, but it seems only one file is being monitored. How can I resolve this issue?
EDIT:
Used exact output by request in the comments.
NOTE: Following answers were based on the
bashtag that was originally attributed to the question; the tag has since been changed tozsh; I don't usezshbut from comments by OP:while/readloop works inzshmapfiledoes not work inzshAs mentioned in comments the current
monitored_pathsassignment populates the variable with a single (2-line) string:Notice the embedded linefeed (
\n) between the 2 paths.This entire 2-line construct is fed to
fswatchas a single path.A couple options for populating
monitored_pathsas an array:while/read loop:
mapfile:
Both of these create/populate the array:
From here OP's current code should function as expected: