I'm looking for an elegant cross-compatible way to direct the names of files into entr. I know the following works with Bash, but I'd like a command that would work in other shells.
(entr is a program that watches files and runs a command on start and when those files change. Its documentation shows ls *.ext, for example, piped into it - so it is expecting a newline separated list of legitimate filenames piped to it. It warns on bad filenames.)
This command lets me list specific files with a multiline herestring:
$ entr echo "command ran" <<< "
> filename1
> filename2
> "
I find the quotation marks and having to list them on separate lines to be annoying. Also a quick search indicates it might not work in shells other than Bash.
I could use interactive cat:
$ cat | entr echo "command ran"
filename2
filename2
but again I have to do a newline each time, not much margin for error, and carefully enter Ctrl-D at the end.
A heredoc works similarly but involves bookend syntax.
$ cat << EOF | entr echo "command ran"
> filename1
> filename2
> EOF
I could also do ls, which looks nice at first glance:
$ ls filename1 filename2 | entr echo "command ran"
But ls would strip out misspelled names, while entr validates and warns on improper names, and I'm worried about side-effects of calling ls in this way.
I could echo and pipe to tr before piping to entr:
$ echo filename1 filename2 | tr " " "\n" | entr echo "command ran"
But I don't like having all the extra commands.
How do I write a short, elegant command that works in most (preferably all) shells? Syntax expected by posix would be ideal.
Since entr appears to want each file on a separate line you're pretty limited. You have to have each filename on a separate line. Any attempt to make it a single line (eg your tr example) will fail if the separator character is part of a filename ("this is a filename").
I'd just do the simple
echo 'filename 1
filename2' | entr echo "command ran"
frobnicate 'echo "command ran"' filename1 filename2orfrobnicate filename1 filename2(andecho "command ran"is fixed) or "I'd like to pick files from the current directory interactively". For now we know few syntaxes you don't like. What would you like? - Kamil Maciorowskiprintf "%s\n" filename1 "filename with spaces 2" | entr echo "command ran"- Mark Plotnick