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?
Asked
Active
Viewed 3,803 times
2
-
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
2 Answers
2
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.

Dave C
- 1,282
- 9
- 15
-
1Input 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, soalias fish='echo'
is enough t makefish hello
work – Bernhard Mar 17 '13 at 20:21 -
-
2The quotes are required in the
echo
argument to preserve the space after the prompt. – depquid Mar 17 '13 at 20:27