Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do this ssh ${w100user}@web100 'ls -l "$(grep "${1}" /etc/pure-ftpd/pureftpd.passwd|cut -d':' -f6)"'

Which obviously performs an ssh session to server web100 as w100user and then greps for the script's first argument "$1" in the file pureftp.passwd, grabbing the 6th field which should be that user's home directory and will then list the contents with a long listing of that user's home directory. I can't seem to get $1 to expand properly before in the command substitution over SSH. I tried multiple quoting methods including '$( "$1" command...)' and at this point it's just guessing.

share|improve this question
up vote 3 down vote accepted

Pass it in a $LC_xxx variable which is one that ssh often passes across:

LC_MY_USER=$1 ssh "${w100user}@web100" '
    ls -l "$(grep -F "$LC_MY_USER" /etc/pure-ftpd/pureftpd.passwd|cut -d: -f6)"'

Or maybe more robustly:

LC_MY_USER=$1 ssh "${w100user}@web100" '
    ls -l -- "$(awk -F: "/\$1 == ENVIRON[\"LC_MY_USER\"] {print \$6; exit}
      " /etc/pure-ftpd/pureftpd.passwd)"'

(assuming you want to check $1 against the first field in that password file).

You could do:

ssh "${w100user}@web100" '
    ls -l "$(grep -F "'"$1"'" /etc/pure-ftpd/pureftpd.passwd|cut -d: -f6)"'

to have the local shell expand $1, but beware that it's potentially dangerous as it's interpreted as shell code by the remote shell (think for instance of $1 being $(rm -rf /)). So you'd have to sanitize $1 first.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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