BASH脚本cd到目录与路径空间

编程入门 行业动态 更新时间:2024-10-21 13:22:06
本文介绍了BASH脚本cd到目录与路径空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Argggg。我一直在用这种愚蠢的问题所困扰了几天,我无法找到答案。

Argggg. I've been struggling with this stupid problem for days and I can't find an answer.

我使用的是Mac OS X的BAS​​H,我想创建一个简单的可执行脚本文件,它的运行时会切换到另一个目录。但是,路径到该目录中有空格。如何赫克你做到这一点?这是我有...

I'm using BASH on Mac OS X and I'd like to create a simple executable script file that would change to another directory when it's run. However, the path to that directory has spaces in it. How the heck do you do this? This is what I have...

文件的名称: CD code 的

文件内容: CD〜/我的code

现在理所当然的,这不是一个很长的路径名,但我的实际路径是五个目录深,这些目录四种路径中有空格。

Now granted, this isn't a long pathname, but my actual pathname is five directories deep and four of those directories have spaces in the path.

顺便说一句,我试过 CD〜/我的code和 CD〜/我的\\ code 键,这些都不奏效。

BTW, I've tried cd "~/My Code" and cd "~/My\ Code" and neither of these worked.

推荐答案

在双引号的路径,你停止波浪线扩展。因此,有几个方法可以做到这一点:

When you double-quote a path, you're stopping the tilde expansion. So there are a few ways to do this:

cd ~/"My Code" cd ~/'My Code'

代字号不在这里引用,所以波浪线扩展仍将运行。

The tilde is not quoted here, so tilde expansion will still be run.

cd "$HOME/My Code"

您还可以扩大双引号字符串内的环境变量;这基本上是什么波浪线扩展是做

You can expand environment variables inside double-quoted strings; this is basically what the tilde expansion is doing

cd ~/My\ Code

您也可以使用反斜杠转义特殊字符(如空格)。

You can also escape special characters (such as space) with a backslash.

更多推荐

BASH脚本cd到目录与路径空间

本文发布于:2023-06-07 05:54:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   路径   目录   空间   BASH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!