How do I execute a remote alias over an SSH?

For instance, there is a rather complex alias for rkhunter, and I would like to execute it in one command over ssh like that:

ssh user@server_ip -p port_number -t alias_name_defined_on_server

To be exact, the alias is defined in .bash_aliases which is sourced by .bashrc of this particular user.


A working answer was published in SO, quoted below:

Quoted from the man page of bash: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt ...

So the simplest way IMO is to put the following lines at the top of your /home/<user>/.bashrc file:

# comment out the original line
# [ -z "$PS1" ] && return

if [ -z "$PS1" ]; then
  shopt -s expand_aliases
  # alias ls='ls --color=always'
  # return

Save and exit. Now you can run ssh user@host "your_alias" successfully.

Note: If your ~/.bashrc hold something like the below, comment the line that return when the shell is not interactive:

# If not running interactively, don't do anything
 case $- in
     *i*) ;;
       *) return;;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.