"cd" space in variable problem!

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
dead76

"cd" space in variable problem!

Post by dead76 »

Hi every one!
I tried to save a long directory into an environmental variable to use it afterwards, but it seems that "cd" command lose its ability to read symbolic characters. For clarity, I put an screen-shot into an attachment. Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: "cd" space in variable problem!

Post by xenopeek »

Code: Select all

cd "$CPP"
That would be the way to do it normally. You should always quote variables used as command line arguments.
Image
Mute Ant

Re: "cd" space in variable problem!

Post by Mute Ant »

You quote them...

Code: Select all

CPP="/home/mute/Desktop/C++/C++ Primer (beginner)"
mkdir -p "$CPP"
cd "$CPP"
pwd
/home/mute/Desktop/C++/C++ Primer (beginner)
dead76

Re: "cd" space in variable problem!

Post by dead76 »

Thank you guys! It worked. However I couldn't understand why the backslash didn't function as a "space character definer " in this case!!
Locked

Return to “Scripts & Bash”