2

I am working on creating a bash alias so I can just cd to a given directory and run a command which opens the pwd. My script works great, but when I grab ${pwd} it grabs the pwd of the bash_profile file. How do I get it to grab the pwd of the calling terminal window?

alias opencoda="osascript -e 'tell application \"Coda\"' -e 'tell document 1' -e 'change local path \"${pwd}\"' -e 'end tell' -e 'end tell'"

SOLUTION I'm not sure really why the above gives the bash_profile dir and this one the terminal dir, but nonetheless:

alias opencoda='osascript -e "tell application \"Coda\"" -e "tell document 1" -e "change local path \"${PWD}\"" -e "end tell" -e "end tell"'

I had to change the quotes around.. also apparently needed to keep double quotes inside there.

Another fun Coda bash script I just wrote:

Open a given file from the current directory:

function coda() {  osascript -e "tell application \"Coda\"" -e "tell document 1" -e "open \"${PWD}/$@\"" -e "end tell" -e "end tell";}

Ex) coda myfile.txt

wejrowski
  • 439
  • 5
  • 20
  • wejrowski, if you've found the answer to your question, it's better if you post it as an answer. Look toward the bottom of the page for the "Answer My Question" link. Don't edit your answer into the question. – David Z Oct 10 '11 at 04:48

2 Answers2

2

When you reference a variable inside a double-quoted string, Bash substitutes the value of the variable right then and there. All you need to do is escape the $, so that the substitution doesn't take place. That way, when you run opencoda, Bash will see the variable reference $PWD in the command and will do the substitution at that time.

alias opencoda="... \${PWD} ..."

(Incidentally, on my computer only $PWD [capitalized] works.)

David Z
  • 128,184
  • 27
  • 255
  • 279
  • So I try this alias opencoda="osascript -e 'tell application \"Coda\"' -e 'tell document 1' -e 'change local path \"\${PWD}\"' -e 'end tell' -e 'end tell'" and it tries to open literally "${PWD}" but If I do this, alias pwdtest="echo \${PWD}" it works correctly.. something funky must be happening with all the other quotes? Or possibly because that specific command is in single quotes?.. Tried playing with it all but no luck... – wejrowski Oct 06 '11 at 20:13
  • My guess is the single quotes. I'd suggest replacing them with double quotes. – David Z Oct 06 '11 at 20:27
  • Yeah that's what I thought, but for some reason that osascript will not run if I switch the quotes. Doesn't make sense. – wejrowski Oct 06 '11 at 20:30
  • Maybe the "change local path" command needs its own copy of double quotes? Try `opencoda="... -e \"change local path \\\"${pwd}\\\"\" ..."` – David Z Oct 06 '11 at 21:40
0

I'm not really sure what that is doing so this is a fairly wild guess, but i would try escaping the $ in the variable \${pwd}. Then on the first parse by .bash_profile is will evaluate it to ${pwd} which should then pass the correct variable.

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90