I do not complain about the absence of such a file or directory

I am new to shell programming on macosx and have a little problem. I wrote the following shell script:

#!/bin/sh function createlink { source_file=$1 target_file="~/$source_file" if [[ -f $target_file ]]; then rm $target_file fi ln $source_file $target_file } createlink ".netrc" 

When I execute this script, I get the message ln: ~ / .netrc: There is no such file or directory , and I do not know why this happened! Do you see a mistake? Thanks!

+4
source share
1 answer

The problem is that tilde expansion does not happen because the path is in a variable value ( tilde expansion occurs before variable expansion ). You can improve this problem by using $HOME instead of ~ . it

 target_file="${HOME}/${source_file}" 

This should solve your problem.

Further reading: EXPANSION section man bash

+5
source

Source: https://habr.com/ru/post/1481515/