How can I use aliased commands with xargs?
Aliases are shell-specific - in this case, most likely bash-specific. To execute an alias, you need to execute bash, but aliases are only loaded for interactive shells (more precisely, .bashrc
will only be read for an interactive shell).
bash -i runs an interactive shell (and sources .bashrc).
bash -c cmd runs cmd.
Put them together:
bash -ic cmd runs cmd in an interactive shell, where cmd can be a bash function/alias defined in your .bashrc
.
find -name \*bar\* | xargs bash -ic gi foo
should do what you want.
Edit: I see you've tagged the question as "tcsh", so the bash-specific solution is not applicable. With tcsh, you dont need the -i
, as it appears to read .tcshrc unless you give -f
.
Try this:
find -name \*bar\* | xargs tcsh -c gi foo
It worked for my basic testing.
xargs doesn't recognize bash aliases
Since only your interactive shell knows about aliases, why not just run the alias without forking out through xargs
?
find . -iname '.#*' -print0 | while read -r -d '' i; do foobar "$i"; done
If you're sure that your filenames don't have newlines in them (ick, why would they?), you can simplify this to
find . -iname '.#*' -print | while read -r i; do foobar "$i"; done
or even just find -iname '.#*' | ...
, since the default directory is .
and the default action is -print
.
One more alternative:
IFS=$'\n'; for i in `find -iname '.#*'`; do foobar "$i"; done
telling Bash that words are only split on newlines (default: IFS=$' \t\n'
). You should be careful with this, though; some scripts don't cope well with a changed $IFS
.
Passing commands to a subshell with xargs does not work inside an alias
The problem is that $1
is getting substituted when you define the alias, rather than when you run it. To prevent that, you need to quote the $
, either by using a backslash, or by using some single-quotes. For example:
alias printgpa='find ~/ -name .git -type d | sed '\''s,/*[^/]\+/*$,,'\'' | xargs -L1 bash -c '\''echo "$1"'\'' _'
xargs command silently fails when used in a bash alias
The issue was with my version of xargs which is supplied with OSX. By using the GNU version, @socowi's suggestion of using a function worked just perfectly.
To install the gnu version of xargs, I did as @Benjamin W said and used homebrew to install findutils. I then added the path as suggested from installing findutils to my ~/.bash_profile.
Make a Bash alias that takes a parameter?
Bash alias does not directly accept parameters. You will have to create a function.
alias
does not accept parameters but a function can be called just like an alias. For example:
myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}
myfunction old.conf new.conf #calls `myfunction`
By the way, Bash functions defined in your .bashrc
and other files are available as commands within your shell. So for instance you can call the earlier function like this
$ myfunction original.conf my.conf
Having an alias with multiple commands, is it possible to pass an argument to the first command?
Define a function to call the two commands instead. The argument to the function can be passed to the first command.
go_to () {
command1 "$1"
command2
}
Related Topics
Ffmpeg Fix Watermark Size or Percentage
Backing Up (And Restoring) a Plone Instance
Bash: Best Architecture for Reading from Two Input Streams
How to Detect Usb Drive Insertion in Linux
Need an Overview of Debugging Process from the Hardware Layer
How to Find a List of Ip Addresses in Another File
Is Kernel Space Mapped into User Space on Linux X86
Why Didn't I Get Segmentation Fault When Storing Past the End of the Bss
How to Disable CPU Cache (L1/L2) on a Linux System
Should %Rsp Be Aligned to 16-Byte Boundary Before Calling a Function in Nasm
How to Tell Qt to Use Different Openssl
Sed Returns "Sed: Command Garbled"
Linux:How to Set Default Route from C
Suppress Notice of Forked Command Being Killed
How to Determine Ssl Cert Expiration Date from a Pem Encoded Certificate
Where Is the Linux Isr Entry Point
How to Find Substring Inside a String (Or How to Grep a Variable)