piping output to an alias

2

1

in order to run a certain command I need to use

sudo -h -u someuser <somecommand>

in order to save some sanity I created an alias

alias somecommand='function _dm(){ sudo -u ubuntu somecommand $@; };_dm'

This works great until I try and do something fancy.

somecommand arg1 | xargs somecommand arg2

This causes me problems since xargs doesn't invoke the alias. Is it possible to make this work somehow?

Jeffrey Ellin

Posted 2015-10-11T00:48:26.840

Reputation: 123

1I'm not sure why you're wrapping a function inside the alias, if you could just use the same function directly? [not that it'd help with xargs but still] – user1686 – 2015-10-11T00:51:19.850

Answers

1

  1. As long as your alias is in your .bashrc, you should be able to do ...|xargs bash -c somealias ...

  2. Easier is to put your alias in a shell script instead. Make ~/bin, add it to your PATH, put the script in, and your original command will work.

Example shell script:

#!/bin/bash
sudo -u ubuntu somecommand "$@"

Edit: this answer suggests trying #1 with bash -ic rather than bash -c.

cxw

Posted 2015-10-11T00:48:26.840

Reputation: 1 389

The problem I am having with #1 is that I run the command like | xargs bash -c somealias param1

somealias behaves as if no additional parameters are passed. Its not seeing the piped in parameters. – Jeffrey Ellin – 2015-10-11T00:56:03.460

May have to do with the details of somecommand. What about #2? It's more reliable and more flexible. – cxw – 2015-10-11T01:14:10.850

1ended up using option 2 with a shell script. Thanks – Jeffrey Ellin – 2015-10-16T13:58:43.190