1

I'm trying to create an alias for an unwieldy command I'm using to output the PID and some other information about two rails servers I have running.

When I simply run the command in terminal (over ssh):

vagrant@Railsvr:~$ ps aux | grep rails | grep -v grep | awk '{print $2, $12, $13, $14, $15}' 

I get the desired info:

3266 bin/rails server -dp 3000
3383 bin/rails s -dp 4000

But when I put it into an alias in ~/.bash_aliases (which is included in ~/.bashrc)

alias findrails_pid="ps aux | grep rails | grep -v grep | awk '{print $2, $12, $13, $14, $15}'"

and source:

. ~/.bashrc

It breaks...I get:

vagrant@Railsvr:~$ findrails_pid 
awk: {print , 2, 3, 4, 5}
awk:        ^ syntax error

It is as though the $n, which represent columns from the ps aux command, are being ripped out for some reason. This is the first alias I have tried to create - am I missing something?

Not sure if this matters - system is an ubuntu precise32 guest box hosted by libvirt-kvm on a Debian 8.1 host.

1
  • 1
    I typically put fancy stuff into shell functions, not aliases.
    – thrig
    Aug 25, 2015 at 18:03

1 Answer 1

5

A function would probably be better, yes, but in this case the problem probably is because you are using double quotes around it. The $ variables get expanded before the assignment to the alias. You can use a single quote on the outside instead, or, because you already have single quotes, escape the $ with \:

alias findrails_pid="ps aux | grep rails | grep -v grep | awk '{print \$2, \$12, \$13, \$14, \$15}'"
3
  • Ah - because single quotes interpret contents literally?
    – starseed
    Aug 25, 2015 at 19:51
  • Yes, and double quotes allow variables to be interpolated into the string.
    – lsd
    Aug 25, 2015 at 19:55
  • That works nicely. Thank you for the assist.
    – starseed
    Aug 25, 2015 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .