-1

This question already has an answer here:

so I have a alias for hadoop which I defined in bash_aliases which bashrc touches upon I guess.

alias hadoop=/usr/local/hadoop-2.7.2/bin/hadoop

Now the alias works fine when I directly try to run it. But when I try this

ssh aditya@localhost hadoop

I get the following error message

bash: hadoop: command not found

I tried the following too

shopt -s expand_aliases

But that didn't work. Could anyone help me out.

Update:

This question is different from mine, because the ssh command is not generated by me, so the solution to that question does not solve my question. The ssh command is generated and executed by a script inside a library I am using. I do not prefer to modify that script to change the command as I am not completely aware of how that script works.

marked as duplicate by Anwar, heemayl bash Aug 27 '16 at 10:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • See the linked duplicate. Basic answer is that aliases only work on local system. You have to define that alias on remote system to make it work there – Sergiy Kolodyazhnyy Aug 27 '16 at 4:36
  • I don't have a remote system. I am ssh into localhost! – Aditya Aug 27 '16 at 4:37
  • Ah, I see. Hold on, I'll post an answer – Sergiy Kolodyazhnyy Aug 27 '16 at 4:39
  • 1
    Why are you ssh'ing to your system in the first place ? Why not use just the regular terminal ? – Sergiy Kolodyazhnyy Aug 27 '16 at 4:46
  • 2
    In the duplicate question, the OP is also ssh-ing to localhost – Zanna Aug 27 '16 at 5:09
1

From man ssh

If command is specified, it is executed on the remote host instead of a login shell.

That implies that shell will run your command as sh -c. That's first of all - this runs in dash, which is default shell and symlinked to /bin/sh. So your alias for bash won't work in the first place.

Second of all, sh -c is non-interactive shell. That means it won't source your ~/.bashrc if you have defined alias there.

But main reason why your command does not work is because you run

alias hadoop=/usr/local/hadoop-2.7.2/bin/hadoop

in the current shell session. It is not exported, nor it is in ~/.bashrc to be sourced to begin with


Here's an alternative. Make alias in your ~/.bashrc:

alias run_hadoop='ssh aditya@localhost /usr/local/hadoop-2.7.2/bin/hadoop'

That way it will be way easier , than defining alias for just command and failing over and over to run it. Alternatively , make a wrapper script run_hadoop.sh in your home directory:

#!/bin/bash
exec /usr/local/hadoop-2.7.2/bin/hadoop

Make sure it is executable with chmod +x run_hadoop.sh. After that you can do

ssh aditya@localhost 'bash /home/user/run_hadoop.sh'
  • Hey, read the update I wrote on my question. Your solution does not work because of that. – Aditya Aug 27 '16 at 4:54
  • @Aditya Well, there's not really much one can do about aliases. You will have to modify your ssh command to source the ~/.bash_aliases , ssh aditya@localhost 'bash -c "source ~/.bashg_aliases; hadoop"'. Sorry, but there's really no way without modifying. – Sergiy Kolodyazhnyy Aug 27 '16 at 5:02
  • Hadoop doesn't need to be an alias. I can make it whatever I want. I can try the path option too, but I am not sure how to do it. I tried path=$path:/usr/local/hadoop-2.7.2/bin like he suggested but that didn't work. Someone in IRC told me to try functions but that didn't work either. – Aditya Aug 27 '16 at 5:08
  • PATH has to be capital. If you are setting it in ~/.bashrc, like i said, that won't work because it's non interactive shell. PATH is only sourced in interactive shells. – Sergiy Kolodyazhnyy Aug 27 '16 at 5:12

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