Reputation: 51
I have a thumb drive named "NO NAME"
I want to write a shell script that will find the path of this thumb drive, and will change the directory into that thumdrive.
The below code will successfully print out /Volumes/NO NAME
export usb=$(system_profiler SPUSBDataType | grep -oE '/Volumes/.*$' | tail -1)
My problem is that when I try to run the below command
cd $usb
I get an error saying theirs no directory named Volumes/NO
Obviously its not picking up "NAME" because their is a space in-between, but how do I fix this? I do not want to ditch the idea of assigning that directory path to a variable then calling the variable to change the directory... and I also cannot change the usb name.
Any help is greatly appreciated!
Upvotes: 0
Views: 57
Reputation: 75629
You need to quote the path:
cd "$usb"
In fact you should always do that if you use variables that shell "unfolds" prior use.
Upvotes: 1