0

I want to write a cript for a user to set an installation path. I am using this

read $file_path
cd $file_path

But it does not change to the path saved on that variable. How can i set this exactly because this seems the wrong way?

2 Answers 2

2

read does not use the $ to read the variable. Hence, it should be

read file_path
cd $file_path
Sign up to request clarification or add additional context in comments.

5 Comments

same problem even when i change to this
@user2250162 Does the path exist? Try to do echo $file;path ls $file_path to see if it gets it properly.
echo prints the path,but cd does not change directory
Do you use absolute or relative path, @user2250162?
Try cd "$file_path", that is, inside quotes.
1

Somewhat reading between the lines, I think you are trying to call a script which you expect to change directory of the caller: For example:

myscript:

read file_path
cd "$file_path"

command-line:

./myscript

and you find it hasn't changed the directory. That's because you are running the script in a child process. It changes the current directory of the child, then returns to the parent, which is unaffected.

You need to source the file instead, that is, run the commands in the current process, not a separate one:

. ./myscript

Yes, notice the extra 'dot' . at the start. This is generically known as the source command, and you can use source instead of 'dot' on bash and csh.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.