Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use variables in an alias?

Status
Not open for further replies.

kmarris

Programmer
Jan 25, 2007
12
0
0
US
From the command line I can execute the following with success:

Code:
ps | awk '{print $1}'

It prints the first column of a ps listing as expected. However, if I bind this command to an alias, it does not handle the "$1" variable correctly. I've tried escaping the variable with a backslash, single quote, and double quote, but that did not solve the problem. How can I set the alias correctly?

I'm using tcsh, but I'm not sure if it matters.


Code:
>alias test "ps | awk '{print $1}'"
>alias test
ps | awk '{print }'

 
the $1 is not a variable, it is part of the awk code

like this it should work:

alias test='ps | awk "{print \$1}"'


HTH,

p5wizard
 
Hey kmarris.

Try adding an equal sign after what you're assigning an alias to, like so:

$ alias test="ps | awk '{print $1}'"
$ test
PID TTY TIME CMD
9711 pts/6 00:00:00 bash
17406 pts/6 00:00:00 ps
17407 pts/6 00:00:00 awk

Hope this helps.

borne2bash

 
I don't know tcsh, but I think you don't want an alias but a function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi, feherke,

I thought I mentioned I was using tcsh.

I tried the syntax you provided, but I was unable to get it working in tcsh. Do you have a solution for my particular shell?

Thanks.
 
Thanks for the replies everyone, but nothing has worked so far.

The equal sign is not valid syntax for defining an alias in tcsh, so that did not help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top