seewalker
seewalker

Reputation: 1243

xargs Behavior : Semantics or Bug?

Wanting to push a number of directories onto the stack, I ran:

echo ~/{Desktop,Downloads,Movies} | xargs pushd and encountered xargs: pushd: No such file or directory

Brace expansion is not the cause of the mismatch between what I have in mind and what happens because echo ~/Desktop | xargs pusdh results in the same error.

As a point of comparison, echo ~/Desktop | xargs cd changes directory as one would expect.

What's going on here?

Upvotes: 1

Views: 124

Answers (2)

Brightshine
Brightshine

Reputation: 965

It's semantics, the equivalent statement should be:

pushd $(echo ~/{Desktop,Downloads,Movies})

After my experiment, the behavior of builtin command is like

#!/bin/sh

    function pushd()
    {
        accept input from $1, $2, $3.....

        # Builtin will not read from stdin! So you can't use pipe.
    }

The builtin command should be viewed as shell function.

[Edit]

The command 'pushd' in zsh is implemented together with 'cd', it only accept one argument. So you can't push a number of directories in single statement. source is there

Upvotes: 1

Nicholas Wilson
Nicholas Wilson

Reputation: 9685

Are you sure xargs cd does what you expect? I'd be surprised! xargs will call a binary, but pushd is not - run type pushd if you want confirmation. cd and pushd don't make much sense as external binaries.

You'll need to capture the directories in a variable, and call pushd in a for loop in the shell process itself, rather than from xargs, which is a child process of the shell, and hence any directory state modified by xargs or its children won't pass up to the parent shell.

Upvotes: 0

Related Questions