In bash, I ask a user to enter the absolute path of a file, such as dir/to/the/file.txt and then go to that directory and run some commands. But I just cant get to the supplied directory.
I've tried the scripts below, and if it doesn't throw me an error, it just outputs a single "."
#!/bin/bash read -p "Please Enter Path:" $r1 parent=$(dirname "${r1}") echo "${scriptdir}" #!/bin/bash read -p "Please Enter Path:" $r1 parent=$(readlink -f "$(dirname "$r1")") cd $parent #!/bin/bash read -p "Please Enter Path:" $r1 parent="$(basename "$(dirname "$r1")")" echo "$parent" What am I doing wrong?
read -p "Please Enter Path:" $r1->read -p "Please Enter Path:" r1. Generally quote variables everywhere,cd "$parent". It outputs a single., becauser1variable is empty, you basically rundirname "", which outputs..readwould store the value being read. An alternative would be to not write anything for a variable, in which case the answer would be stored in the special variableREPLY. For instanceread -p "Enter Path"; parent=$(dirname $REPLY).