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:
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.
RELATED NEWS AND ANALYSIS
-
Huawei’s AI Update: Things Are Moving Faster Than We Think
FEATURE | By Rob Enderle,
December 04, 2020
-
Keeping Machine Learning Algorithms Honest in the ‘Ethics-First’ Era
ARTIFICIAL INTELLIGENCE | By Guest Author,
November 18, 2020
-
Key Trends in Chatbots and RPA
FEATURE | By Guest Author,
November 10, 2020
-
Top 10 AIOps Companies
FEATURE | By Samuel Greengard,
November 05, 2020
-
What is Text Analysis?
ARTIFICIAL INTELLIGENCE | By Guest Author,
November 02, 2020
-
How Intel’s Work With Autonomous Cars Could Redefine General Purpose AI
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
October 29, 2020
-
Dell Technologies World: Weaving Together Human And Machine Interaction For AI And Robotics
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
October 23, 2020
-
The Super Moderator, or How IBM Project Debater Could Save Social Media
FEATURE | By Rob Enderle,
October 16, 2020
-
Top 10 Chatbot Platforms
FEATURE | By Cynthia Harvey,
October 07, 2020
-
Finding a Career Path in AI
ARTIFICIAL INTELLIGENCE | By Guest Author,
October 05, 2020
-
CIOs Discuss the Promise of AI and Data Science
FEATURE | By Guest Author,
September 25, 2020
-
Microsoft Is Building An AI Product That Could Predict The Future
FEATURE | By Rob Enderle,
September 25, 2020
-
Top 10 Machine Learning Companies 2020
FEATURE | By Cynthia Harvey,
September 22, 2020
-
NVIDIA and ARM: Massively Changing The AI Landscape
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
September 18, 2020
-
Continuous Intelligence: Expert Discussion [Video and Podcast]
ARTIFICIAL INTELLIGENCE | By James Maguire,
September 14, 2020
-
Artificial Intelligence: Governance and Ethics [Video]
ARTIFICIAL INTELLIGENCE | By James Maguire,
September 13, 2020
-
IBM Watson At The US Open: Showcasing The Power Of A Mature Enterprise-Class AI
FEATURE | By Rob Enderle,
September 11, 2020
-
Artificial Intelligence: Perception vs. Reality
FEATURE | By James Maguire,
September 09, 2020
-
Anticipating The Coming Wave Of AI Enhanced PCs
FEATURE | By Rob Enderle,
September 05, 2020
-
The Critical Nature Of IBM’s NLP (Natural Language Processing) Effort
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
August 14, 2020