BASH脚本cd到目录与路径空间 [英] BASH Script to cd to directory with spaces in pathname

查看:1482
本文介绍了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到目录与路径空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆