Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Hi I was wondering I have to run some rather long commands all day. I know how to create aliases for the commands, but the problem I am having is each command contains different IP address in the middle of the command. Is there a way to create an alias so that it runs the command and all I would have to do is enter an IP?

share|improve this question

marked as duplicate by n0pe, jasonwryan, Gilles, vonbrand, Renan Mar 17 '13 at 22:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I don't believe this is a full duplicate. OP question was how to read input from the console and use the input within an alias. The duplicate question linked was how to use parameters with an alias and suggests implementing a function - not updating the alias itself to ask for user input. – Dave C Mar 17 '13 at 23:38

Another alternative would be to create a function, rather than an alias, as that saves you running a sequence of commands.

function doip
  my_command "$1"
share|improve this answer

Yes a few different ways; with read and chaining things together or a function (function description in a different answer).

To use read and chain:

read IP && echo $IP

Will read a line from the console (stored in $IP) and then echo it out.

So the alias command:

alias fish='read IP && echo $IP'

Will do the same when you type fish.

So let's say you want to: prompt for IP, read IP, ping IP with the alias fish.

The command would be:

alias fish='echo -n "Enter IP: " && read IP && ping $IP'

fish will prompt for IP (Enter IP:), read an IP into $IP and then ping it.

share|improve this answer
Input prompt can be made a little prettier using echo's -n switch, where supported: alias fish='echo -n "Enter IP: " && read IP && ping $IP' – depquid Mar 17 '13 at 20:12
The alias just expands on the command line, so alias fish='echo' is enough t make fish hello work – Bernhard Mar 17 '13 at 20:21
Just realised that and removed - whoops. – Dave C Mar 17 '13 at 20:24
The quotes are required in the echo argument to preserve the space after the prompt. – depquid Mar 17 '13 at 20:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.