0

Bonjour, I'm trying to do something in bash, here is the code :

var1=Hello
var2=I want a coffee
function func () {          
var_temp="alias $1='$2'"          #I need ='' in the result
echo $1
echo $2 
echo $var_temp; }
sshpass -e ssh -q $USER@host "$(declare -f func);func" $var1 $var2
Result $1=Hello
Result $2=I
Result $var_temp=alias Hello='I'

How can I export more than one word in ssh ? Thank you in advance, #be understanding for my english xD

4
  • What are you doing with the output of func? If you want to add something to a file, just construct the string locally and pipe it to something like ssh $USER@host 'cat >> the_file'. Commented Feb 23, 2022 at 16:43
  • 1
    Before worrying about ssh, get the local things fixed: var2=I want a coffee needs quotes or escapes to protect the spaces, and all variable references should be double-quoted (e.g. echo "$var_temp" instead of just echo $var_temp). shellcheck.net is good at pointing out missing double-quotes. As for ssh, my answer here may help. Commented Feb 23, 2022 at 16:45
  • E.g. printf "alias %s='%s'\n" "$var1" "$var2" | ssh ... Commented Feb 23, 2022 at 16:45
  • WIth the output, I want to add string in a file with a sed at a specific lines "after" ``` sudo sed -i "text /a $var_alias" /etc/.....sh ``` Commented Feb 24, 2022 at 9:13

1 Answer 1

0

Try this

#!/usr/bin/env bash
  
var1=Hello
var2="I want a coffee"
func(){                     
    var_temp="alias $1='$2'"      
    echo "$1"
    echo "$2"
    echo "$var_temp"
}
sshpass -e ssh -q $USER@host "$(declare -f func);func" "'$var1'" "'$var2'"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.