Am 04.04.2012 17:27, schrieb jrrand...@gmail.com:
On Tue, Apr 3, 2012 at 5:22 PM, jrrand...@gmail.com<jrrand...@gmail.com> wrote:
Hi everyone,
In bash, is it possible to expand my aliases either before they are executed
or when they are stored in the history file?
For example, if I have: alias ll='ls -l' defined in my .bashrc, when I
execute "ll" from the command line, I'd like my history file to contain "ls
-l". Is there any way to accomplish this?
Thanks,
Justin
I seem to have constructed a solution to my own problem. I wrote a
function that creates the desired behavior for me and saves it in
$expanded if the argument was in fact an alias.
function expand_alias() # expand an alias to full command
{
if [ "$1" = "." ]; then
argument="\\$1"
else
argument="$1"
fi
match=$( alias -p | grep -w "alias $argument=" )
if [ -n "$match" ]; then
expanded="`echo $match | sed -e s/[^=]*=// | sed 's/^.\(.*\).$/\1/'`"
else
expanded="$1"
fi
}
Thanks,
Justin
or you could just do
function expand_alias() # expand an alias to full command
{
if expanded=$( alias "${1}" ); then
expanded="${expanded#*=}"
else
expanded="$1"
fi
}
though depending on what your actually trying to do you'd be getter of
using type, as not all aliases are aliases a lot are functions these days.
case $(type -t "$c") in
"")
echo No such command "$c"
return 127
;;
alias)
c="$(type "$c"|sed "s/^.* to \`//;s/.$//")"
;;
function)
c=$(type "$c"|sed 1d)";\"$c\""
;;
*)
c="\"$c\""
;;
esac
bash -xvc "$c"