Ezra
Ezra

Reputation: 1481

using xargs to pass a variable to alias command

I'm trying to write a one liner that creates an alias 'cd="cd dir_name"' which will change directory to to that dir_name

pwd | xargs -i alias cd{}='cd $PWD'

but I get:

xargs: alias: No such file or directory

is it that alias cannot be played with xargs or am I not using xargs correctly?

Upvotes: 1

Views: 622

Answers (1)

choroba
choroba

Reputation: 242048

alias is a shell builtin. xargs needs an external command to run. Normally, you can run a new shell in xargs to interpret the builtins or keywords:

pwd | xargs -i bash -c 'alias cd{}="cd $PWD"'

but it's useless in this case, as the alias would live only in the shell you run from xargs, not in the current one.

Moreover, alias can't be named /home/user. Maybe you meant

... alias cd='cd {}'

Use pushd and popd to remember the current directory and return to it later.

Upvotes: 2

Related Questions