Reputation: 411
I have this in my .bashrc
:
alias jpsdir="jps | awk '{print $1}' | xargs pwdx"
but when I use jpsdir
I get this output:
pwdx: invalid process id: JvmName
but running
jps | awk '{print $1}' | xargs pwdx
gives the correct results:
1234: /some/dir/
What is wrong with my alias? Should i make it a function ?
Upvotes: 6
Views: 7419
Reputation: 1
Been trying to run : date | awk '{print $5}'
as a command to extract the time from the date command.
When I run "time" as a command, I get this gibberish:
time
real 0m0.000s
user 0m0.000s
sys 0m0.000s
So the time is the 5th string item in the date command and I was so pleased when I plucked it out using an awk command, my first attempt at using awk. As Tom French's answer suggests if you get rid of the $x above, the command prints the entire date string. It didn't take me very many attempts using the alias command before I came upon a pretty simple example that wouldn't run.
Upvotes: 0
Reputation: 74635
As gniourf_gniourf has explained in the comments, the reason that your alias wasn't working was because the $1
in your awk command was being expanded by the shell. The value is likely to be empty, so the awk command becomes {print }
and pwdx
is being passed both parts of the output of jps
.
You can avoid having to escape the $
by avoiding using awk entirely; you can use the -q
switch rather than piping to awk:
jpsdir() {
jps -q | xargs pwdx
}
I would personally prefer to use a function but you can use an alias if you like.
Upvotes: 10