As others have said, "alias" doesn't process the command line arguments given on the command line that invokes the "aliased" command. It simply passes along all the command line arguments to the substituted command by appending the arguments to the end.
Here is an example test script to show this effect...
Start with a script like this:
[~]# cat /some...path/ls1.sh
#!/bin/sh
dollarpound="$1"
shift
dollarone="$1"
shift
echo ""
echo "dollarpound=\"$dollarpound\""
echo "dollarone=\"$dollarone\""
echo "\$*=\"$*\""
echo "\$@=\"$@\""
echo "\$1=\"$1\""
echo "\$2=\"$2\""
echo ""
if [ $# -eq 0 ]; then
echo ls --color -lh *
else
echo "ls --color -lh $*"
fi
echo ""
[~]#
Then, assign that script to an alias:
[~]#alias lsx='/some...path/ls1.sh $# "$1"'
[~]#
Invoke the alias lsx
with no arguments:
[~]# lsx
dollarpound="0"
dollarone=""
$*=""
$@=""
$1=""
$2=""
ls --color -lh ant bat cat dog eel.txt fox.txt goat.jpg horse.jpg
[~]#
Invoke the alias lsx
with 1 argument: "*.txt"
:
[~]# lsx *.txt
dollarpound="0"
dollarone=""
$*="eel.txt fox.txt"
$@="eel.txt fox.txt"
$1="eel.txt"
$2="fox.txt"
ls --color -lh eel.txt fox.txt
[~]#
Notice that the script invoked by the "aliased" command reports 0
arguments instead of 1
("*.txt"
), or 2
("eel.txt", "fox.txt"
).
Invoke the alias lsx
with 4 arguments:
[~]# lsx ant bat cat dog
dollarpound="0"
dollarone=""
$*="ant bat cat dog"
$@="ant bat cat dog"
$1="ant"
$2="bat"
ls --color -lh ant bat cat dog
[~]#
Notice that the script invoked by the "aliased" command reports 0
arguments instead of 4
.
So, a working version of the script is:
[~]# cat /some...path/ls2.sh
#!/bin/sh
if [ $# -eq 0 ]; then
\ls --color -lh *
else
\ls --color -lh $*
fi
[~]#
(Notice the \
before ls
. This prevents ls
from being further substituted by other "aliases", etc...).
Assign the working script to an alias:
# alias lsx='/some...path/ls2.sh'
[~]#
Invoke the alias lsx
with no arguments:
[~]# lsx
-rw-r--r-- 1 someuser someuser 0 Jan 21 02:42 eel.txt
-rw-r--r-- 1 someuser someuser 0 Jan 21 02:42 fox.txt
-rw-r--r-- 1 someuser someuser 0 Jan 21 02:42 goat.jpg
-rw-r--r-- 1 someuser someuser 0 Jan 21 02:42 horse.jpg
ant:
total 0
bat:
total 0
cat:
total 0
dog:
total 0
[~]
Notice that the script detected there were no arguments and ran "ls *"
instead of "ls"
(with no arguments).
Invoke the alias lsx
with 1 argument: "*.txt"
:
[~]# lsx *.txt
-rw-r--r-- 1 someuser someuser 0 Jan 21 02:42 eel.txt
-rw-r--r-- 1 someuser someuser 0 Jan 21 02:42 fox.txt
[~]
Invoke the alias lsx
with 4 arguments:
[~]# lsx ant bat cat dog
ant:
total 0
bat:
total 0
cat:
total 0
dog:
total 0
[~]#
If there is a reason you want to do this with an "alias", this should work for you. Just just give the alias and the script whatever names you like. For example, if you wanted to "alias" the ls
command, use alias ls='/some...path/ls2.sh'
(with the path and name you used for the script).
On the other hand, if you want this command to be uniquely named, like lsx
for example, it might be simpler to just give the script that name, and put the script in a folder that is included in the path, like: /bin/lsx
, or whatever you like.