bash_profile 别名 -- 使用 PWD 打开 Coda

发布于 2024-12-09 02:22:38 字数 813 浏览 0 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

起风了 2024-12-16 02:22:38

当您引用双引号字符串内的变量时,Bash 会立即替换该变量的值。您需要做的就是转义 $,这样替换就不会发生。这样,当您运行 opencoda 时,Bash 将在命令中看到变量引用 $PWD 并在那时进行替换。

alias opencoda="... \${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 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.)

生活了然无味 2024-12-16 02:22:38

我不太确定它在做什么,所以这是一个相当疯狂的猜测,但我会尝试转义变量 \${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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文