How to pass all values from a list to Run Shell Script action?

I’m trying to pass all values from an action whose result is a list to the Run Shell Script action and then process those list values within the script. But for some reason, nothing gets passed into it.

Here is a simple example using Get Selected Files in Finder as the source, plus a screenshot of it below.

If I select a few files in the Finder, then run this, I see all of the file paths in the first Show Result action. But the second Show Result action simply shows “0”. The command cat - | wc -l should just send stdin to cat and then wc -l should count the number of lines. If I do this from the Terminal it works fine.

I’m on macOS Ventura 13.0 and Shortcuts 6.0

Does anyone see what I’m doing wrong here? Thanks!

I believe Shortcuts is only passing the first file of the selection in to stdin for the script step, or at least when I selected a few files with this shortcut (however, I get non-zero results when I tried with some test files), I can see it exhibiting this behaviour :wink:

Switching to using arguments rather than stdin, and changing the base instruction to be cat "$@" | wc -l seems to produce the desired result.

In the inline documentation, there’s certainly some ambiguous messaging and it doesn’t quite fit what I’m seeing.


“Item(s)” looks to only be the plural for arguments, but the “Pass Input” section does note that for stdin, “The input will be convereted to a file” (singular) … though I would not describe an operation of taking the first item in a list as “converted” except in very specific circumstances.

Hope that helps.

Thanks for the two examples! They were quite helpful and switching to using “Pass input: as arguments” is working now. I’m actually doing this in Python and sys.argv contains the full set of list items (although it has a leading -c argument because I assume Shortcuts is launching it as python -c <script> <args>).

It’s odd to me that when using “stdin” and I use the “Show Results” action, it sees and displays the full list, but the script action only sees the first item in the list, but I’m learning that Shortcuts currently has some edges…

I haven’t tested this idea, but it might be possible to use the Combine Text action to convert the list of filenames into a single piece of text before passing it as input to the shell script via stdin.