title: Change script directory to user's homedir in a shell script
body: <p>In my bash script I need to change current dir to user's home directory. </p>
  
  <p>if I want to change to user's <em>foo</em> home dir, from the command line I can do:</p>
  
  <pre><code>cd ~foo
  </code></pre>
  
  <p>Which works fine, however when I do the same from the <em>script</em> it tells me:</p>
  
  <pre><code>./bar.sh: line 4: cd: ~foo: No such file or directory
  </code></pre>
  
  <p>Seams like it would be such a trivial thing, but it's not working. What's the problem here? Do I need to escape the "~" or perhaps missing quotes or something else? </p>
  
  <p><b>Edit</b></p>
  
  <p>when I say <em>user</em> I don't mean <em>current</em> user that runs the script, but in general any <em>other</em> user on the system</p>
  
  <p><b>Edit</b></p>
  
  <p>Here is the script:</p>
  
  <pre><code>#!/bin/bash
  
  user="foo"
  cd ~$user
  </code></pre>
  
  <p>if username is hardcoded like</p>
  
  <pre><code>cd ~foo
  </code></pre>
  
  <p>it works, but if it is in the <em>user</em> variable then it doesn't. What am I missing here?</p>