4

I'm new to shell scripting and having a little trouble.

I'm trying to execute a remote script from local machine. The script on local machine is passed four arguments, and it executes the following line:

ssh $STACK 'bash -l -c "./deploy_intermediate.sh $2 $3 $4"'

However, I cannot get $2, $3, and $4 to put out the passed arguments. Is there any way to be able to access them within single quotes? What is the best way of executing the above line.

I would really appreciate any help.

thanks.

2 Answers 2

6

The trick is:

ssh -i ~/.ssh/"$GIT_PRIVKEY" user@"$IP" "bash -s" < localpath/script.sh "$arg1" "$arg2"
Sign up to request clarification or add additional context in comments.

1 Comment

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". See mywiki.wooledge.org/Quotes, mywiki.wooledge.org/Arguments and wiki.bash-hackers.org/syntax/words .
1

To pass the parameters you'd have to write:

ssh $STACK 'bash -l -c "./deploy_intermediate.sh '$2 $3 $4'"'

Thus you make way for the $i values to be expanded. Yet, I think you'd probably be able to get the same result by simply running:

ssh $STACK "./deploy_intermediate.sh $2 $3 $4"

2 Comments

But it will have surprising results if any of those arguments have whitespace or shell metacharacters in them.
@rici Then escaping each variable with double quotes should suffice. Something like: ssh ... "./command \"$1\" \"$2\" ...".

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.