share
Unix & LinuxList specific files in command that expects `ls` piped into it
[0] [1] Aaron Hall
[2020-04-15 02:26:09]
[ pipe posix ]
[ https://unix.stackexchange.com/questions/580128/list-specific-files-in-command-that-expects-ls-piped-into-it ]

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.)

multiliners

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

one-liners:

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.

The Question:

How do I write a short, elegant command that works in most (preferably all) shells? Syntax expected by posix would be ideal.

So how exactly would you like to specify files? A situation where we craft different methods, trying to fit your undisclosed subjective taste, does not correspond with the gist of the site. You should state your requirements first, e.g frobnicate 'echo "command ran"' filename1 filename2 or frobnicate filename1 filename2 (and echo "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 Maciorowski
(3) If all you need are newline-separated names, maybe this will be ok: printf "%s\n" filename1 "filename with spaces 2" | entr echo "command ran" - Mark Plotnick
@MarkPlotnick that should be in an answer - Aaron Hall
[+2] [2020-04-15 03:00:10] Stephen Harris

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"

1