Trouble with zypper and pipe

Tumbleweed 20230519
In my bash script (or directly on command line) when i do this:
cmd="zypper --non-interactive dup --allow-vendor-change --auto-agree-with-licenses | grep -v -E ‘Retrieving|In cache|Installing:’

zypper fails complaining there are too many arguments

if I shorten the cmd to
cmd=“zypper --non-interactive dup --allow-vendor-change --auto-agree-with-licenses”
then its ok.
I cant for the life of me see whats wrong here
Can anyone point me in the right direction, I want to use this in a script

This is what I get:

henk@boven:~> cmd="zypper --non-interactive dup --allow-vendor-change --auto-agree-with-licenses | grep -v -E ‘Retrieving|In cache|Installing:’

that ends with the secundary prompt because the " is not matched.

You seem to think that after $cmd is replaced by it’s contents, the whole interpretation by bash is starting from the beginning. This is not the case. Looking for a pipe (|) is already done way before the replacing of $cmd. Thus the | is just that: |.

Compare with:

henk@boven:~> cmd="ls -l | grep rwx"
henk@boven:~> $cmd
ls: cannot access '|': No such file or directory
ls: cannot access 'grep': No such file or directory
ls: cannot access 'rwx': No such file or directory

The lesson of this is that you should always try to re-create by using a simple construction. And do not blame zypper of course :wink:

ok, but what is the solution?

cmd="zypper | grep -v -E ‘Installing | dracut | Retrieving’ "

Is there a way to make this work?

sorry, the mismatch was only because i didnt copy correctly, my bad

Generally you’ll want to wrap this inside a function:

cmd () {
    zypper | grep -v -E 'Installing | dracut | Retrieving'

Well, it works. It (the shell) does what you tell it to do.

Maybe that is not what you want it to do. But when you want help about what you want, you should explain what you want. E.g. why first storing the command in a variable and then (trying to) execute it immediately after. It is obvious that just directly using the command would do the zypper as you like it. But there must be more that made you trying to use this strange construct. And as long as we do not understand this, it is difficult to help. @awerlang’s suggestion is also based on some assumption (and it is also a good hint). But answers based on assumptions are often not helping.