A little confused with how you want output to look, but should be easy enough to change from below script (if you want just command paths/aliases, just change out="$i"
to out=""
). Note, bash isn't particularly good at handling aliases in shell scripts, so you have to source whatever files you keep them in.
#!/bin/bash
ali() {
arg="$*"
input=$(echo "$arg"| tr ' ' '\n')
save=""
while read i; do
out=$(type "$i" 2>/dev/null)
if [[ $out == *"aliased to"* ]]; then
out=${out%%\'*}
out=${out##*\`}
out=$(ali "$out")
elif [[ $out == *"$i is"* && $out != *"builtin"* && $out != *"keyword"* ]]; then
out=${out##*"$i is"}
else
out="$i"
fi
save="$save $out"
done <<< "$input"
echo "$save"
}
shopt -s expand_aliases
source ~/.bashrc
ali "$1"
Example output
$ ./script "find . -type f | x grep 'linux' | wc -l"
/usr/bin/find . -type f | /usr/bin/xargs /bin/grep 'linux' | /usr/bin/wc -l
$ ./script "[[ -f test.txt ]] && ls"
[[ -f test.txt ]] && /bin/ls
$ ./script ":> test.txt"
:> test.txt
$ ./script "ll"
/bin/ls -lhtr
Some bug with spacing/escaping somewhere, but should be easy enough to fix with an sed
, or just echo -e $(./script "whatever")
should work here.
Example output with out="" instead of out="$i" and hackish spacing fix
$ echo -e $(./script "find . -type f | x grep 'linux' | wc -l")
/usr/bin/find /usr/bin/xargs /bin/grep /usr/bin/wc
$ echo -e $(./script "[[ -f test.txt ]] && ls")
/bin/ls
$ echo -e $(./script ":> test.txt")
$ echo -e $(./script "ll")
/bin/ls
Update
Exact output you want shouldn't be too hard to change in-script. But simpler, change out="$i"
to out=""
and do (or make a wrapper to do it). Also note, I added save=""
to the script above, since there was a slight bug with $save getting kept somewhere and the first argument repeated.
$ echo -e $(./script "find . -type f | x grep 'linux' | wc -l") | tr ' ' '\n'
/usr/bin/find
/usr/bin/xargs
/bin/grep
/usr/bin/wc
$ echo -e $(./test.sh "find . -type f | x grep 'linux' | wc -l; where ls") | tr ' ' '\n'
/usr/bin/find
/usr/bin/xargs
/bin/grep
/usr/bin/wc
/bin/ls