0

Whenever I execute this script it says: no such file or directory. I'm not sure what I'm doing wrong here. I put quotes around it just in case if there is a space in the directory's name.

#!/bin/bash
read -p "Enter destination: " folder
folder=$(sed -e 's/^/"/' -e 's/$/"/' <<< $folder)
cd $folder
2

1 Answer 1

1

It is better to use quotes in the cd command, regardless of whether the directory has spaces or not, like this:

#!/bin/bash
read -p "Enter destination: " folder
cd "$folder"
pwd

Test:

enter image description here

Another solution (use with caution as it may cause other problems) is using eval in your code:

#!/bin/bash
read -p "Enter destination: " folder
folder=$(sed -e 's/^/"/' -e 's/$/"/' <<< $folder)
eval cd $folder

References:

Sign up to request clarification or add additional context in comments.

2 Comments

Please don't use eval like this, it just adds ways for things to go weirdly wrong.
Thanks, i read this reference ( unix.stackexchange.com/questions/278427/… ) and I recognize that eval without the corresponding sanitization is evil.

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.