bash_profile 别名 -- 使用 PWD 打开 Coda
我正在创建一个 bash 别名,这样我就可以 cd 到给定目录并运行打开 pwd 的命令。我的脚本运行得很好,但是当我抓取 ${pwd} 时,它会抓取 bash_profile 文件的 pwd。如何让它获取调用终端窗口的密码?
alias opencoda="osascript -e 'tell application \"Coda\"' -e 'tell document 1' -e 'change local path \"${pwd}\"' -e 'end tell' -e 'end tell'"
解决方案 我不确定为什么上面给出了 bash_profile 目录,而这个给出了终端目录,但尽管如此:
alias opencoda='osascript -e "tell application \"Coda\"" -e "tell document 1" -e "change local path \"${PWD}\"" -e "end tell" -e "end tell"'
我必须更改周围的引号..显然还需要在其中保留双引号。
我刚刚编写的另一个有趣的 Coda bash 脚本:
从当前目录打开给定文件:
function coda() { osascript -e "tell application \"Coda\"" -e "tell document 1" -e "open \"${PWD}/$@\"" -e "end tell" -e "end tell";}
Ex) coda myfile.txt
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您引用双引号字符串内的变量时,Bash 会立即替换该变量的值。您需要做的就是转义
$
,这样替换就不会发生。这样,当您运行opencoda
时,Bash 将在命令中看到变量引用$PWD
并在那时进行替换。(顺便说一句,在我的计算机上只有
$PWD
[大写] 有效。)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 runopencoda
, Bash will see the variable reference$PWD
in the command and will do the substitution at that time.(Incidentally, on my computer only
$PWD
[capitalized] works.)我不太确定它在做什么,所以这是一个相当疯狂的猜测,但我会尝试转义变量
\${pwd}
中的$
。然后,在 .bash_profile 第一次解析时,会将其评估为${pwd}
,然后它应该传递正确的变量。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.