Programing

경로 이름에 공백이있는 디렉토리로 cd 할 Bash 스크립트

lottogame 2020. 9. 21. 22:18
반응형

경로 이름에 공백이있는 디렉토리로 cd 할 Bash 스크립트


macOS X에서 Bash를 사용하고 있으며 실행시 다른 디렉터리로 변경되는 간단한 실행 스크립트 파일을 만들고 싶습니다. 그러나 해당 디렉토리의 경로에는 공백이 있습니다. 도대체 어떻게하는거야? 이것이 내가 가진 것입니다 ...

파일 이름 : cdcode

파일 내용 :

cd ~/My Code

이제는 긴 경로 이름이 아니지만 실제 경로 이름은 5 개의 디렉토리 깊이이고 그 중 4 개는 경로에 공백이 있습니다.

BTW, 나는 해봤 cd "~/My Code"cd "~/My\ Code"와 어느 쪽도 이들의이 일을하지 않습니다.


경로를 큰 따옴표로 묶으면 물결표 확장이 중지됩니다. 따라서이를 수행하는 몇 가지 방법이 있습니다.

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

여기서 물결표는 인용되지 않으므로 물결표 확장은 계속 실행됩니다.

cd "$HOME/My Code"

큰 따옴표로 묶인 문자열 내에서 환경 변수를 확장 할 수 있습니다. 이것은 기본적으로 물결표 확장이하는 일입니다.

cd ~/My\ Code

백 슬래시로 특수 문자 (예 : 공백)를 이스케이프 할 수도 있습니다.


이 페이지에서 아래 해결책을 찾았습니다 .

x="test\ me"  
eval cd $x

\큰 따옴표로 묶인 텍스트 상수와 eval이전 의 조합은 cd매력처럼 작동합니다!


cd ~/My\ Code

나를 위해 작동하는 것 같습니다 ... 따옴표를 삭제했지만 슬래시를 유지하는 것이 작동하지 않으면 샘플 코드를 게시 할 수 있습니까?


다음 중 하나를 사용할 수 있습니다.

cd ~/"My Code"
cd ~/M"y Code"
cd ~/My" Code"

다음을 사용할 수 없습니다.

cd ~"/My Code"

첫 번째 방법은 쉘이 ~ /를 $ HOME /으로 확장 한 다음 큰 따옴표없이 내 코드를 택하기 때문에 작동합니다. 두 번째는 매핑 할 ' "'(큰 따옴표) 라는 사용자가 없기 때문에 실패 ~"합니다.


단일 백 슬래시가 작동합니다.

ry4an@ry4an-mini:~$ mkdir "My Code"

ry4an@ry4an-mini:~$ vi todir.sh

ry4an@ry4an-mini:~$ . todir.sh 

ry4an@ry4an-mini:My Code$ cat ../todir.sh 
#!/bin/sh
cd ~/My\ Code

Are you sure the problem isn't that your shell script is changing directory in its subshell, but then you're back in the main shell (and original dir) when done? I avoided that by using . to run the script in the current shell, though most folks would just use an alias for this. The spaces could be a red herring.


After struggling with the same problem, I tried two different solutions that works:

1. Use double quotes ("") with your variables.

Easiest way just double quotes your variables as pointed in previous answer:

cd "$yourPathWithBlankSpace"

2. Make use of eval.

According to this answer Unix command to escape spaces you can strip blank space then make use of eval, like this:

yourPathEscaped=$(printf %q "$yourPathWithBlankSpace")
eval cd $yourPathEscaped

Avoid ~ in scripts; use $HOME instead.


When working under Linux the syntax below is right:

cd ~/My\ Code

However when you're executing your file, use the syntax below:

$ . cdcode

(just '.' and not './')


use double quotes

go () 
{ 
    cd "$*"
}

The very simple way of doing this is-

 $ cd My\ Folder

In bash, run DIR command and in the results you would see that the folder or path names having space between them has been written in the results like this -

$dir
My\ Folder
New\ Folder

I had a similar problem now were I was using a bash script to dump some data. I ended up creating a symbolic link in the script folder with out any spaces in it. I then pointed my script to the symbolic link and that works fine.

To create your link. ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]

Mau or may not be of use.


Use single quotes, like:

myPath=~/'my dir'

cd $myPath

참고URL : https://stackoverflow.com/questions/589149/bash-script-to-cd-to-directory-with-spaces-in-pathname

반응형