I often want to edit the files resulting from find
or fd
like so:
fd myfile | my_script
In my script, vim
would be run with all the files from STDIN as arguments like vim "myfile1" "myfile2"
. The arguments need to be individually double-quoted since they may include spaces & other special characters.
I've tried the following:
files=""
while IFS=$'\n' read -r line; do
files="$files $line"
done
file $files
The example should run file
with the resulting file names as arguments. This kinda works, except having white spaces in the file names break it, and I've tried quoting the variables multiple ways with no success.
How can I run a command with newline-separated inputs as arguments with spaces?
xargs
? – LL3 Apr 15 '21 at 11:41mapfile
/readarray
or put them in an array manually, see https://mywiki.wooledge.org/BashGuide/Arrays and https://www.gnu.org/software/bash/manual/html_node/Arrays.html and https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters – ilkkachu Apr 15 '21 at 11:53echo foo | xargs vim
gives you the warning messageVim: Warning: Input is not from a terminal
(and then you have to press ^C to get back to the shell). Command substitution works, though - e.g.vim $(echo foo)
– cas Apr 15 '21 at 12:41xargs
at all. BTW, recent GNU's and BSD'sxargs
have-o
to avoid that inconvenience for interactive programs. – LL3 Apr 15 '21 at 13:12-o
option sounds interesting and useful, I missed seeing that had been introduced. – cas Apr 16 '21 at 11:58