How to make alias shortcuts for terminal work as root or as exec?

Posted on

Problem :

Let’s say I have an alias like this:

alias findip='wget -q -O -|sed -e 's/.*Current IP Address: //' -e 's/<.*$//''

the output would look like this:

matthew@ubuntu:~$ findip

So pretty simple, it finds my ip address by connecting to the website and looking for my IP address. Now, if i were to run:

tsocks wget -q -O -|sed -e 's/.*Current IP Address://' -e 's/<.*$//'

I would get an output like this:

matthew@ubuntu:~$ tsocks wget -q -O -|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

You will notice that the IP addresses are different. This is because I routed the command through tsocks, which I have configured to use tor. But if I were to run:

tsocks findip

My output would be this:

matthew@ubuntu:~$ tsocks findip
exec: 87: findip: not found

My output

How do I configure my system so that the aliases created apply when I am running through tsocks?

Solution :

Alias a command to itself, and end the alias with a space:

alias exec="exec "
alias sudo="sudo "
alias tsocks="tsocks "

With this, bash will automatically expand tsocks findiptsocks wget -q ...

As seen in bash’s documentation:

$ help alias
alias: alias [-p] [name[=value] ... ]
    A trailing space in VALUE causes the next word to be checked for
    alias substitution when the alias is expanded.

This, however, is not related to being root (unless you were talking about sudo usage), and cannot be done at exec() syscall level (aliases are entirely internal to bash).

Leave a Reply

Your email address will not be published.