Passing Arguments to a bash shell

Hi all,

I need some help trying to figure out how to send different arguments to a bash script.
I thought this would work but I can’t seem to get the individual arguments by themselves. “$@”, and “$1” seem to get everything, $2 gives an error.

Ta

Everything is implicitly being passed in as one argument, so will only match $1. You have one bit of text, so that is one argument; it has been implicitly wrapped in double quotes.

If you want to pass multiple arguments, you need to split that one thing into separate things - a list in Shortcuts parlance.

Note also that while your title is “bash”, you have set the shortcut to use a “zsh” shell. It is also worth explicitly setting your input to ensure it is definitely what you expect.

hope that helps.

Ah I see - thank you. I’d obviously (wrongly) assumed that the script would split the arguments on spaces by itself.

I’ve also found that you can enter variables directly into the bash/zsh script by right clicking and selecting “insert variable” so no need to pass them in as arguments.

Yes - if you search the forum you’ll see that inserting magic variables into scripts comes up several times in topics touching on shall scripting in Shortcuts.

There are cases for both approaches. I typically use direct variable insertion, but for unknown numbers of variables I’ll vary between looping inside and outside of the script, depending upon volumes and data types for efficiency’s sake.