4

I can't get this command to work:

user@server:~: ssh otherserver bash -ic 'source .profile; some-aliased-command'

Variations I tried

user@server:~: ssh -t otherserver bash -ic 'source .profile; some-aliased-command'
user@server:~: ssh -t otherserver "bash -ic 'source .profile; some-aliased-command'"
user@server:~: ssh -t otherserver "bash -ic source .profile; some-aliased-command"
user@server:~: ssh otherserver bash -ic 'source .profile; some-aliased-command'
user@server:~: ssh otherserver "bash -ic 'source .profile; some-aliased-command'"
user@server:~: ssh otherserver "bash -ic source .profile; some-aliased-command"

I usually get a variation of these errors:

bash: cannot set terminal process group (-1): Invalid argument
bash:  no job control in this shell
stdin: is not a tty
bash: some-aliased-command: command not found

But like this, it does work:

user@server:~: ssh otherserver bash -i << EOF
source .profile
some-aliased-command
EOF

In fact when I run this:

ssh -t otherserver 'bash -ic "source ~/.profile; alias"'

It lists all the aliases including the some-aliased-command that I want to run.

I tried al sorts of variations with single, double, escaped quoutes, but I am stuck and it will only work with the heredoc version.

How do I make it work without the heredoc ( as a one-liner ) ?

migrated from serverfault.com Jun 18 '15 at 9:14

This question came from our site for system and network administrators.

  • 1
    Maybe .profile contains commands that require a tty. Why are you sourcing .profile in the first place? – kasperd Jun 17 '15 at 9:50
  • @kasperd Because I need an alias that is in there. The .profile and .bashrc files are not under my control. I can edit them, but they could be reset at any time. – Willem D'Haeseleer Jun 17 '15 at 10:09
  • Aliases do not belong in .profile. Aliases are local to the shell and .profile is only sourced by login shells. So most shells would not have any access to those aliases. If you have aliases in .profile, then you are doing it wrong. – kasperd Jun 17 '15 at 10:13
  • @kasperd Then why does it work when I do ssh -t otherserver 'bash -ic "source ~/.profile; alias"' or use the heredoc syntax ? How is it different ? – Willem D'Haeseleer Jun 17 '15 at 10:16
  • We might be able to figure out why that convoluted command makes a difference. But I would say it is pointless. Put the aliases where they belong, then there will be fewer unknowns to figure out. – kasperd Jun 17 '15 at 10:19
1

Same answer as previous, but add +m for job control issue fix:

ssh otherserver "bash -ic +m 'source .profile; some-aliased-command'"

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.