bash: ~ expansion inside shell script -


i tried write simple shell script build pipeline analysis , 1 step involves building symlink existing directories , files.

initially set this:

dir='~/project/xxxx/' ln -s $dir . 

however, above code didn't work.

i figured out ~ didn't expanded inside shell script when it's executed.

i tried use double quote instead of single quote around directory path didn't work either.

the following worked

dir="$home/project/xxxx/" ln -s $dir . 

i'm wondering why ~ didn't work. what's formal name phenomenon?

thanks!

tilde expansion not occur inside quotes of kind. so, example, use instead:

dir=~'/project/xxxx/' 

note if use ~username/ format, username/ part must unquoted. man bash:

tilde expansion

   if word begins unquoted tilde character (`~'),   of    characters preceding first unquoted slash (or charac‐    ters, if there no unquoted slash) considered  tilde-pre‐    fix.   if  none of characters in tilde-prefix quoted,    characters  in   tilde-prefix  following   tilde     treated possible login name.  if login name null    string, tilde replaced value of shell parame‐    ter  home.  if home unset, home directory of user exe‐    cuting shell substituted instead.  otherwise,   tilde-    prefix   replaced  home directory associated    specified login name. 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -