Changing directories using variables.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing directories using variables.
# 1  
Old 11-07-2008
Changing directories using variables.

I can't seem to solve this problem. SmilieSmilie Please assist. Thanks!

#!/bin/bash

UserDir="$(echo ~$1)"

echo "Changing directory with variables"
cd "$UserDir"
echo "Changing directory without variables"
cd ~pearsn
pwd


Output:
pearsn$ sh -x ./test.bash pearsn
++ echo '~pearsn'
+ UserDir='~pearsn'
+ echo 'Changing directory with variables'
Changing directory with variables
+ cd '~pearsn'
./test.bash: line 6: cd: ~pearsn: No such file or directory
+ echo 'Changing directory without variables'
Changing directory without variables
+ cd /Users/pearsn
+ pwd
/Users/pearsn
# 2  
Old 11-07-2008
Code:
cd ~$1

# 3  
Old 11-07-2008
Same result. No luck. I've tried this as well

cd UserDir=$(echo ~$1)
cd UserDir=`echo ~$1`
cd UserDir="~$(echo $1)"
# 4  
Old 11-07-2008
You cant put ~$1 together you need to add it like ~/$1
# 5  
Old 11-07-2008
~ (tilde) will expand to user home directory(current user).
Code:
# echo '~'
~
# echo "~"
~
# echo ~
/usr/danmero

If you wrapping with quotes or double quote will prevent expansion.
# 6  
Old 11-07-2008
The answer is to use "eval" but the reason is not easy to explain. I'll give it a try:

The shell evaluates commandlines on a fixed step-by-step basis. For instance you rely implicitly on this mechanism when you write "var=$(somecommand $othervar)" as you do in your script.

The shell has somehow to follow a recipe to expand "$othervar" prior to "$(...)", because the other way round the command would get a literal "$othervar" as an argument instead of the content of the variable.

For the same reason the following will not work:

Code:
var="something"
varname="var"
echo $$varname

We could expect the shell to first expand "$varname" to "var" and then expand "$var" to "something" - but this is not the case because all variables are expanded in the same step and therefore at the same time, not one after the other.

For the same reason your code fails: the step where "~" is expanded to the home directory is already over when you try to evaluate it and therefore you are left with a literal "~pearsn" instead of the home directory of the user.

Still, there is good news: you can restart the evaluation process with step 1 by using the keyword "eval" - in fact this is the reason for the existence of this keyword. Lets start with the variables example from above:

Code:
var="something"
varname="var"
eval echo \$$varname

The shell would - because finding nothing else making sense - the "$$" expand to the PID, which is why we will have to protect it with the backslash. So the first time the line is evaluated it goes like this:

(eval)=keyword (echo)=keyword (\$)=a "$" which is meant to remain one ($varname)=oops, we have to expand that one -> replace with "var"

Since the "eval" is there the resulting line is now evaluated again (without the "eval" now):

(echo)=keyword ($var)=oops, needs expanding -> "something"

And as you will notice this works in the expected way.

What does this mean to your problem? Try the following:

Code:
#!/bin/bash

UserDir="$(echo ~$1)"

echo "Changing directory with variables"
eval cd "$UserDir"

echo $PWD

I hope this helps.

bakunin

PS: Notice, that the evaluation process is slightly different in ksh and in bash. Your script would have worked in Korn shell, but fails in bash, because the steps are laid out a little differently.
This User Gave Thanks to bakunin For This Post:
# 7  
Old 11-07-2008
Thank you all for replying, especially bakunin for the thorough explanation.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Connecting and changing variables in Bash script

#!/bin/bash X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H) Y=$(</home/cogiz/topcardinplay.txt) # KS A=( "${Y::1}" ) B=( "${Y:1}" ) for e in ${X}; do if ]; then # searching for valid cards K,S or 8 ... (0 Replies)
Discussion started by: cogiz
0 Replies

2. Shell Programming and Scripting

need help with creating directories and variables

i'm trying to write a script that has 2 variables, and uses the 1st variable as a number and the 2nd a name to create directories. so if you typed in ./myscript 5 week, it would create 5 directories named week1 - week5. whenever i run this, i get an error message saying week5 already exists, so i... (3 Replies)
Discussion started by: layne2kim
3 Replies

3. Shell Programming and Scripting

Multiple Variables in Array from Existing Directories

I would like to extract directories from a specific place and keep them into an array of variables to run functions into it. Example, bash-3.00$ls adrian bryan caren derrick I want to keep each directory names into a variable adrian --> document bryan --> document caren --> document... (3 Replies)
Discussion started by: lynxlee
3 Replies

4. UNIX for Dummies Questions & Answers

Changing Directories

How can i create a file, for example with a touch command, in a different directory from the current one i am in, in one single line command? (1 Reply)
Discussion started by: glock1800
1 Replies

5. Shell Programming and Scripting

Changing variables in a loop

I'm having a spot of trouble. I'm trying to test three variables for a NULL value in a bash shell script. If a null value is detected in the variable to set it to set the variable to a default value. here is what I have: testResponseA=3 testResponseB= testResponseC=4 for test in... (5 Replies)
Discussion started by: kaltekar
5 Replies

6. Solaris

Changing default 'FORMAT' variables.

In FORMAT->ANALYZE->SETUP there's a couple variables you can set for the various functions. However, everytime I exit format it reverts back to the defaults. Is there a file I can edit somewhere to change these default settings? (0 Replies)
Discussion started by: cheetobandito
0 Replies

7. Shell Programming and Scripting

Need help in changing Permissions to 775 for files and directories

Hi All I need to create a script which would change Permissions to 775 All the Files and directories will be mentioned in the Paramter files Can anyone give a Hint how to proceed in this ?? THanks (1 Reply)
Discussion started by: ranga27
1 Replies

8. Shell Programming and Scripting

changing environment variables

hi friends, i'm new to shell scripting,can i know how to change the environment variables without altering anythng in .bash_profile as the change in it is for a specific user but i want the change to be available to every user who logs in. bye. (1 Reply)
Discussion started by: amit4g
1 Replies

9. Solaris

help in changing the access level for directories

Hi all, can some one help me in chmod command, and let me know the various combinations for this command. for : eg chmod -R 777 <dir names> this gives all rights to all but i want the specific access levels kindly help me out in this issue. Thank you, lakshmanan (2 Replies)
Discussion started by: lakshmananl
2 Replies

10. UNIX for Dummies Questions & Answers

changing directories (i'm sure there is a simple solution for this)

I just want to exit my script in a new directory from a bash shell. Problem is that the script internally changes to the directory I want to move to, however when exits is still in the original directory. Does that make sense? ie usage: goto null changing from /usr/bin/xtra/test/test3/ ... (8 Replies)
Discussion started by: Shakey21
8 Replies
Login or Register to Ask a Question