Datamation Logo

Aliases and Variables Keep Things Short and Simple

September 4, 2008
Datamation content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Aliases are great for saving typing time, and you probably already have a handful set up. But you may not be aware that it’s possible to set up aliases that take command line variables as an argument. Read on for
an example.

Note: You can set
up the commands given below either directly from the command line or by
putting them into your shell config file (e.g., ~/.bashrc or
~/.tcshrc) and then sourcing that.

Occasionally, I want to check which desktop belongs to a particular
user. I have a local LDAP directory value set for this, so the command

ldapsearch "(localUser=username)"

does the trick.
I want to set up an alias for this, with the
command line argument taking the place of username.

In tcsh, this can be done straightforwardly using
!:1 for the first argument, !:2 for the second, and so on.
Remember, you’ll need to escape the ! in the alias definition.

So for my LDAP search:

alias finduser 'ldapsearch "(localUser=!:1)"'

Then the following:

$ finduser username

Unfortunately, in bash/sh/ksh this is no good,
as you can’t put arguments in aliases. What you can do to produce
the same effect is write a function.

function finduser() { ldapsearch "(localUser=$1)"; }

(Note the ; at the end of the command.)

To pass in further arguments to bash, you use $2, $3 and so on. Or, to pass in all the arguments on the command line, $*. This
gives much more scop for saving your typing fingers!

This article was first published on EnterpriseITPlanet.com.

  SEE ALL
ARTICLES
 

Subscribe to Data Insider

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more.

Datamation Logo

Datamation is the leading industry resource for B2B data professionals and technology buyers. Datamation's focus is on providing insight into the latest trends and innovation in AI, data security, big data, and more, along with in-depth product recommendations and comparisons. More than 1.7M users gain insight and guidance from Datamation every year.

Advertisers

Advertise with TechnologyAdvice on Datamation and our other data and technology-focused platforms.

Advertise with Us

Our Brands


Privacy Policy Terms & Conditions About Contact Advertise California - Do Not Sell My Information

Property of TechnologyAdvice.
© 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.