简体   繁体   中英

How to pass bash variable to cmd?

how can I pass $line to the cmd command?

#!/bin/bash
while read line           
do           
    sshpass -p "..." ssh -o StrictHostKeyChecking=no -tt windows@172....-p 333  cmd /c "cd C:\ & download_give_id.exe '$@$line' "

done <apps.txt   

Basically, if you want to interpolate a variable into a bash string you need to use double quotes instead of single quotes:

str="${var} foo bar" # works
str='${var} for bar' # works not

In the special case that you are running ssh commands inside a while loop, I strongly recommend to pass /dev/tty explicitly as input to the command since once the remote command should read from stdin - for whatever reason - it will slurp stdin of the while loop otherwise:

while read line ; do
    ssh ... -- "${line} ..."  < /dev/tty # Pass tty to stdin
done < input.txt

Note: The above command will work only if the process has been started in a terminal. If the process is not running in a terminal, you need to pass something else as stdin for the inner ssh command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM