Trouble using cd command with "~" or "$HOME" in bash scripting
Solution 1
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
Solution 2
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.
Solution 3
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
Related videos on Youtube
Pandya
Started using Linux and StackExchange since Ubuntu 12.04 LTS. Then Upgraded to 14.04 LTS. Now I am using Debian GNU/Linux on my Laptop and PureOS on old Desktop computer. I recommend visiting the Philosophy of GNU Project As I've replaced Ubuntu with Debian GNU/Linux, Now my question(s) are became off-topic on AskUbuntu. So, I continue to Unix & Linux. The second reason for my shifting to U & L is I found U & L more interesting than AU since AU is only Ubuntu specific whereas U & L is a broad concept and in my opinion U & L deserves generic questions. (I know why SE has AU & U & L both).
Updated on September 18, 2022Comments
-
Pandya over 1 year
We know that
~
and$HOME
refer to the home directory of the current user. (For meecho ~
=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? -
choroba almost 10 years@Pandya: Fixed a typo.
-
Run CMD almost 10 years@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? -
Alaa Ali almost 10 yearsI would love to see an explanation of what the
eval
command means and does. -
Pandya almost 10 yearsYes it is working but more simple with @nux answer!
-
Pandya almost 10 years@nux ok.
eval
is helpful to use. -
Run CMD almost 10 years@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.
-
choroba almost 10 yearsJust don't try entering
; rm -rf /
as the path. -
Mitch almost 10 yearsOne 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 /
-
Qwertie over 5 years"in your code $dir1 will not store ~/Desktop" yes it will - that's the problem!