11

So I basically am trying to overwrite my ssh command so I only have to type ssh and by default it would connect to my main server. Then if I passed it an argument, say username@server_port it would then run the basic command.

# Fast SSH  (a working progress) TODO: make work without naming the function `fssh`
function fssh() {

    ALEX_SERVER_CONNECTION=$ALEX_SERVER_UNAME@$ALEX_SERVER_PORT

    # if the `ssh` argument is not set
    if [ -z "${1+xxx}" ]; then
        # echo "ALEX_SERVER_CONNECTION is not set at all";
        ssh $ALEX_SERVER_CONNECTION
    fi

    # if the `ssh` argument is set
    if [ -z "$1" ] && [ "${1+xxx}" = "xxx" ]; then
        ssh $1
    fi
}

How do I get it to work without the f in front of the ssh?

So basically this is how it looks when properly done:

# Fast SSH
function ssh() {

    ALEX_SERVER_CONNECTION=$ALEX_SERVER_UNAME@$ALEX_SERVER_PORT

    # if the `ssh` argument is not set
    if [ -z "${1+xxx}" ]; then # ssh to the default server
        command ssh $ALEX_SERVER_CONNECTION
    fi

    # if the `ssh` argument is set
    if [ -z "$1" ] && [ "${1+xxx}" = "xxx" ]; then # ssh using a different server
        command ssh $1
    fi
}
Alex Cory
  • 10,635
  • 10
  • 52
  • 62
  • I was thinking if I included the path to where the `ssh` is defined, I could include something like `source /path/to/ssh` and it might work, but I can't find the right path to try. – Alex Cory Aug 20 '14 at 07:35
  • Can you provide error message if any? Did you place your function in `~/.bashrc`? – Édouard Lopez Aug 20 '14 at 08:08
  • @AlexCory could you clarify what is your goal as it is ambiguous and leading to totaling different answers – Édouard Lopez Aug 20 '14 at 08:44

3 Answers3

19

Solution

You need to specify the absolute path to ssh command in your function otherwise it will be recursive. For instance, instead of:

function ssh() { ssh $USER@localhost; } # WRONG!

You should write:

function ssh() { command ssh $USER@localhost; }

Use command built-in to get the ssh from the PATH (as suggested by @chepner):

command [-pVv] command [arg ...]

  Run  command  with  args  suppressing  the normal shell function
  lookup. **Only builtin commands or commands found in the PATH  are
  executed**.

Why not Alias?

Using a function is the correct pattern, read the Aliases and Functions sections from the man page.

ALIASES

There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below).

Diagnostic

When naming your custom function ssh do the following:

  1. be sure to reload your shell configuration: source ~/.bashrc
  2. check what is ssh with: which ssh or type ssh

type and which

Prior to declaring a custom function I got:

type ssh  # → ssh is /usr/bin/ssh
which ssh # → /usr/bin/ssh

After declaring function ssh() { ssh my-vm; }, I got:

which ssh # → ssh () { ssh my-vm; }
type ssh  # → ssh is a shell function

Advices

Either use sh or bash syntax:

  • sh: test is done with [ … ], portable but not powerful ;
  • bash test is done [[ … ]] and function keyword, less portable but dev-friendly.
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • 1
    In place of an absolute path to `ssh`, you can also use the `command` builtin, which retains the ability to do path lookup while avoiding a recursive call to the function. `function ssh() { command ssh $USER@localhost; }` – chepner Aug 20 '14 at 13:19
2

How do I get it to work without the f in front of the ssh?

Set an alias in ~/.bashrc:

alias ssh='fssh'
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Say your script is called fssh and is executable, you just have to:

alias ssh='fssh'

in your .bashrc

n0p
  • 3,399
  • 2
  • 29
  • 50
  • A solution could be to copy/paste your code in a script `fssh`, get rid of `function` declaration, make it executable with `chmod +x fssh` and the alias will work. – n0p Aug 20 '14 at 08:46
  • Why an external script? If you leverage `alias` mechanism, you can also leverage `function`s so why bother with external script for a user-scope customization? – Édouard Lopez Aug 20 '14 at 08:49
  • I don't see how is it bothering, I rather find it more handy to visualize your customization and toggle it. – n0p Aug 20 '14 at 09:00