adding a function to .zshrc changes the command

Viewed 131

I added the following function to my .zshrc

function jptt(){
  # Forwards port $1 into port $2 and listens to it
  ssh -N -f -L localhost:$2:localhost:$1 remoteuser@remotehost
}

then I am running jptt 1 2

and get the following error:

Bad local forwarding specification localhost:2ocalhost:1

It is strange that I lose :l after the 2

the function is working when as I tried to replace the command with a simple line and it worked. I also run the ssh command separately and it works well.

2 Answers

The expression $x:l applies the lower-casing modifier to your x variable. The following example illustrates this:

pax> x=ABC
pax> echo $x:lnnn
abcnnn

pax> echo ${x}:lnnn
ABC:lnnn

The first section gives you the lower-case variant, and therefore the modifier is not considered part of your output string. The second section shows how you can prevent this variable expansion by using braces to ensure the :l is not treated as a modifier. In your specific case, that would be done with the line:

ssh -N -f -L localhost:${2}:localhost:${1} remoteuser@remotehost

It's actually a good idea to get into the habit of bracing parameter names as much as possible since there are other cases where this might adversely affect you.

Use ${1} and ${2} . Zsh supports : csh string modificators and :l has special meaning (to lowercase variable in front of it) and that's why it is consumed from $1:localhost.

Related