Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

We know that ~ and $HOME refer to the home directory of the current user. (For me echo ~=echo $HOME=pandya).

But I can't use it in bash scripting. Here is a simple example script:

#!/bin/bash
echo -n "Enter Directory Path:"
read dir1
cd $dir1

But When executed, it gives error No such file or directory as follows:

$ ./script
Enter Directory Path:~/Desktop
./script: line 4: cd: ~/Desktop: No such file or directory

If /home/pandya used instead of ~ ,then it is working.

Same problem with $HOME.

Thus, How to properly use cd with ~ or $HOME in such bash scripting?

share|improve this question
    
Hint: if input starts with "~" replace "~" with "/home/$HOME". –  Rinzwind Aug 12 at 10:20
    
@Pandya now try my answer ! –  nux Aug 12 at 10:58

3 Answers 3

up vote 2 down vote accepted

You can solve this problem by using eval command :

#!/bin/bash
echo -n "Enter Directory Path:"
read dir1
eval cd "$dir1"

Because in your code $dir1 will not store ~/Desktop but it will store /home/user/Desktop so you can use eval command .

To understand Eval command Here

share|improve this answer
1  
I would love to see an explanation of what the eval command means and does. –  Alaa Ali Aug 12 at 11:02
    
@nux ok. eval is helpful to use. –  Pandya Aug 12 at 11:09
2  
Just don't try entering ; rm -rf / as the path. –  choroba Aug 12 at 16:00
1  
One thing about eval, as @choroba alluded to - eval is very dangerous. If you're using this for your own private use, don't worry about it. If you're giving this to someone else to use you should either warn them to be careful with it OR (and this is the better option) put in some sort of checking to make sure that they entered a directory and not something else, like ; rm -rf / –  musher Aug 12 at 17:15

Here's an example which works.

As said before, the expansion of variables from input is the key.

#!/bin/bash
echo -n "Dir:"
read dir1
dir2=`eval echo $dir1`
cd $dir2
pwd

Of course, you should not expect that your current shell will change its working directory after script execution. It will remain unaffected.

share|improve this answer
    
@Pandya Yes it is changing the working directory. But not in the calling shell environment. Why would you want to have a script which does what cd does? –  Class Stacker Aug 12 at 10:46
    
Yes it is working but more simple with @nux answer! –  Pandya Aug 12 at 11:05
    
@Pandya You know as well as I do that my comment referred to an earlier comment of yours, which you deleted in the meantime, in which you claimed that my solution does not work. –  Class Stacker Aug 12 at 15:15

The problem is the tilde expansion happens before variable expansion (see man bash for details). Variable expansion happens just once, so $dir1 is expanded, but the string $HOME inside it is not.

It might be easier to specify the directory path as a command line argument instead of using read to read it from the console: the shell will expand it for you:

#!/bin/bash
dir1=$1
cd "$dir1"
pwd

and call it like

./script ~/Desktop

Another alternative is to use a file dialog instead of typing the path at all:

dialog --dselect / 20 20
share|improve this answer
    
@Pandya: Fixed a typo. –  choroba Aug 12 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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