I have ~/bin
in my PATH
and keep a short script with my favorite defaults accessible by all shells on my system (at different times I use Dash, Bash, and Fish). It's tiny:
#!/usr/bin/env sh
exec xargs -rd\\n "$@"
The script is called x
to avoid conflicts with scripts expecting the standard xargs
defaults. If you try to replace xargs
itself, which I don't recommend, make sure your ~/bin
appears higher in your PATH than the system xargs
. which -a xargs
tells me that the only xargs
exists at /usr/bin/xargs
in my system so I know my home directory, /home/stephen/bin
, must appear before it like so:
$ echo "$PATH"
/home/stephen/bin:...:/usr/bin:...
Either way, as a script accessible by all programs, this means you can do things like find|x grep
and sh -c 'x ...'
.
If you use Bash and prefer an alias, you can also just use:
alias x=xargs -rd\\n\ # \n delim, don't run on empty in, + alias expansion
Note the trailing space for alias expansion. This lets you chain aliases. For example, if in addition to the above alias I had an alias for grep called g
, the following would work:
# extended regex, skip binaries, devices, sockets, & dirs, colored, & line
# -buffered. use a non-canonical alias instead of GREP_OPTIONS which may wreck
# assuming scripts
alias g='grep -EID skip -d skip --color=auto --line-buffered'
$ find|x g foo
The x
/ xargs
script approach can't do this effectively by itself.
Since I switch between shells and use one PC mostly, I keep the handful of aliases I need as separate scripts in ~/bin
and shell agnostic aliases in a helper script, ~/.sh_aliases
, which is sourced by ~/.shrc
, ~/.bashrc
, and ~/.config/fish/config.fish
as they all support a Bash-like alias syntax. If I worked on multiple PCs regularly, I would probably try to consolidate these into ~/.bashrc
instead.