2

I want to write a script that will change to different directories depending on my input. something like this:

test.sh:
#!/bin/bash
ssh machine001 '(chdir ~/dev$1; pwd)'

But as I run ./test.sh 2 it still goes to ~/dev. It seems that my argument gets ignored. Am I doing anything very stupid here?

2 Answers 2

5

Bash ignores any variable syntax inside the single-quoted(') strings. You need double quotes(") in order to make a substitution:

#!/bin/bash
ssh machine001 "(chdir ~/dev$1; pwd)"
Sign up to request clarification or add additional context in comments.

Comments

2

The parameter is enclosed in single quotes, so it isn't expanded on the local side. Use double-quotes instead.

#!/bin/bash

ssh machine001 "chdir ~/dev$1; pwd"

There's no need for the (...), since you are only running the pair of commands then exiting.

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.