0

I am trying to run this script -

#!/bin/bash

my_opts="-2 -o StrictHostKeyChecking=no -o FallBackToRsh=no -q -i /root/.ssh/identity"
alias ssh='ssh $my_opts'

type ssh

It is giving me the output as ssh is /usr/bin/ssh. What needs to be changed so that the output is - ssh -2 -o 'StrictHostKeyChecking no' -o 'FallBackToRsh no' -i /root/.ssh/identity

Thanks in advance.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ira
  • 547
  • 4
  • 13
  • String interpolation does not happen when you use `'`. Try `"` instead. – Jeff Holt Dec 17 '21 at 23:21
  • 3
    Also, aliases are not expanded in non-interactive shells in bash, unless you `shopt -s expand_aliases`. Use a function instead. – choroba Dec 17 '21 at 23:22
  • Thanks Jeff. Modifying `alias ssh='ssh $sshopts'` to `alias ssh="ssh $sshopts"` didn't make any difference. The output was same. – Ira Dec 17 '21 at 23:29
  • What are you trying to do? For what you show, the fix would be to just run `ssh` with the options directly. Why the indirections? – Benjamin W. Dec 17 '21 at 23:33
  • 3
    See [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050), and specifically [5. I'm constructing a command based on information that is only known at run time](http://mywiki.wooledge.org/BashFAQ/050#I.27m_constructing_a_command_based_on_information_that_is_only_known_at_run_time) – David C. Rankin Dec 17 '21 at 23:44

2 Answers2

2

Forget the alias, and define a function.

ssh () {
    command ssh -2 -o StrictHostKeyChecking=no -o FallBackToRsh=no -q -i /root/.ssh/identity "$@"
}
chepner
  • 497,756
  • 71
  • 530
  • 681
-2

By using ' you are creating a literal string also know in many languages as string as a literal - bash is no exception. Or known as a string interpol. You need to use " in order to pass a var through a string unless you surround the bar with ' like so

alias install='install '$var''

Reference https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html

Also making sure you're using source e.g. source ./script.sh on your script and not running using bash ./script.sh when typing the command

  • 4
    That's not the problem here; using single-quotes prevents the variable from expanding when the alias gets defined, but instead it gets expanded when the alias is used. Also, your example leaves `$var` unquoted in the definition command, which can cause trouble (depending on which shell is used, and what the variable's value is). – Gordon Davisson Dec 17 '21 at 23:49
  • @GordonDavisson https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html says otherwise – Charles Porth Dec 17 '21 at 23:52
  • 2
    That only applies when the alias is defined (i.e. when the `alias` command is executed). But the quotes are removed during processing of that `alias` command, so they aren't included in the actual alias, so when the alias gets *used* they aren't there. Try running (in an interactive shell) `var=initialval`, then `alias foo='echo $var'`, then `var=otherval`, and finally `foo` (note that you should run them one-at-a-time rather than as a single line with semicolons, because the order of expansion gets weird in a single line). Then try with `alias foo='echo '\''$var'\'` and note the difference. – Gordon Davisson Dec 18 '21 at 01:13
  • 2
    The OP's core problem is that aliases are not expanded in a script (that is, when the shell is not interactive), _by default_. Quotes are not relevant to the core issue here. – M. Nejat Aydin Dec 18 '21 at 01:23
  • 1
    @CharlesPorth, if the OP were getting to a place where this was relevant, the output of `type ssh` in their script would be something like `ssh is aliased to 'ssh $my_opts'` (and your answer would be focused on changing the `my_opts` to the actual code with options added). But the problem this answer fixes is not the problem the OP has. – Charles Duffy Dec 18 '21 at 20:25
  • 1
    @CharlesPorth, ...the other thing is that `alias install='install '$var''` is exceedingly silly -- it's a needlessly wordy and slightly buggy way to say `alias install="install $var"` (buggy because the contents of `$var` are expanded unquoted and thus unprotected from word-splitting or glob expansion). It would be less incorrect to write `alias install='install '"$var"`, but still rather silly. – Charles Duffy Dec 18 '21 at 20:30