Integralist
Integralist

Reputation: 2211

My shell script to symlink a file from one directory to another is broken

I have a file within a Dropbox directory and I want to symlink it to my home/user directory.

I'm trying to automate it with a shell script like so...

db="$HOME/Dropbox/Fresh\ Install"
ln -s $db/README.md $HOME/symlinked.txt

...but all that happens is when I run sh provision.sh I get the error ln: /Users/<user>/symlinked.txt: No such file or directory

I'm guessing the problem is related to the path pointing to a folder whose name has a space inside it.

I've tried different variations to get it to work but can't figure it out.

Tried to use "command substitution" as well but couldn't understand how it was relevant to the problem.

Any help to get this working appreciated.

Note: I'm using zsh but as far as I'm aware running this simple bash script shouldn't be an issue/conflict.

Upvotes: 0

Views: 273

Answers (1)

Barmar
Barmar

Reputation: 781096

Backslashes aren't processed when expanding variables. You need to quote the variable.

db="$HOME/Dropbox/Fresh Install"
ln -s "$db"/README.md $HOME/symlinked.txt

In general, it's best to always quote variables unless you know you want the value to be subjected to word splitting and wildcard expansion.

Upvotes: 2

Related Questions