5

In my bash script I need to change current dir to user's home directory.

if I want to change to user's foo home dir, from the command line I can do:

cd ~foo

Which works fine, however when I do the same from the script it tells me:

./bar.sh: line 4: cd: ~foo: No such file or directory

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?

Edit

when I say user I don't mean current user that runs the script, but in general any other user on the system

Edit

Here is the script:

#!/bin/bash

user="foo"
cd ~$user

if username is hardcoded like

cd ~foo

it works, but if it is in the user variable then it doesn't. What am I missing here?

3
  • Do you want the home directory for the user foo, or the directory foo in the user's home directory?
    – Scottie T
    Feb 6, 2009 at 17:24
  • I want home directory of user foo Feb 6, 2009 at 17:28
  • What does your shebang look like? Feb 6, 2009 at 17:37

4 Answers 4

15

What about

cd $(getent passwd foo | cut -d: -f6)

and

USER=foo
eval cd ~$USER

works, too (foo is the username)

1
  • the first one (perhaps even with backticks instead of $() ) is more portable since not every shell supports cd ~USER. Feb 6, 2009 at 17:51
8

Change it to:

cd $HOME

Actually, I'm not sure why cd ~whatever wouldn't work. I've just tested with a small script and it worked fine:

#!/bin/bash

cd ~sbright

I actually get the same error message that you do when the specified user does not exist on the system. Are you sure (and yes, I know this is one of those is-it-plugged-in questions) that the user exists and has a valid home directory specified?

Edit:

Now that I see what you are actually doing... tilde expansion happens before variable interpolation, which is why you are getting this error.

3
  • When I mean user I don't mean the current user who runs the script, but any OTHER user on the system Feb 6, 2009 at 17:23
  • Could you post more of your script? Feb 6, 2009 at 17:26
  • Yes user exists, I know that simply because same exact thing works from command line Feb 6, 2009 at 17:43
0

Is the script going to be run by the user? If it is you can just do: cd ~

2
  • No no, script is run by root user and doing operations on other users. Feb 9, 2009 at 20:15
  • Bash must be interpreting the tilde and the variable in some way that we don't expect. After a wee bit of trail and error this works: USER=~foo; cd $USER;
    – Mick T
    Feb 10, 2009 at 4:25
-1

Is there some reason you can't do:

#!/bin/bash

cd /home/$USER

Of course directories aren't in /home on all *nixes, but assuming you know what OS/distro your script is targeted for, you should be able to come up with something that works well enough.

2
  • Thanks for quick answer but this /home/$USER is simply not going to work due to our configuration. Using "~" in my environment is the only feasible way to lookup the user's home. Feb 6, 2009 at 17:33
  • More generally, while /home is the usual path to home directories, it is not guaranteed. I've seem /u/home and /home/user[1-9]/ and other arrangements. Feb 6, 2009 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.