Solved

correct expansion of ~ char in a bash script

Posted on 2001-08-24
7
217 Views
Last Modified: 2010-04-21
Hello-

I'm writing a simple bash script to back up user accounts on a Linux machine. I want to pass the user name into the script as an argument and then expand it to the full home path for the user. In other words, I want to inside the
script do something like:

cd ~user1

This style works fine in the script. However, I need to be able to pass the user name in as an argument, so I'm trying to do:

cd ~$1

This doesn't expand correctly.

How do I get the desired expansion using the argument?

Thanks,

Paul T.
0
Comment
Question by:titchener
7 Comments
 
LVL 4

Expert Comment

by:sdussinger
ID: 6424173
Years ago there used to be a program on Unix called 'glob' which would expand paths like this. It appears that this program has been subsumed into the shell in Linux, so just calling that is not an option. Instead, try these on for size. There are two different ways to do it (1 clean and simple, the other a vicious hack)....

-----------------------------------------------------------
#!/bin/bash

#Version 1
#Extract the home directory for the user from the passwd file.
DIR=`cat /etc/passwd | grep $1 | awk -F: '{print $6}'`
echo Changing to: $DIR
cd $DIR
pwd

#Version 2
#This one is a hack. Create a subshell and let it expand the
#arguments for us. After expansion, it changes to the home
#directory, which we print and store back in the parent
#shell. Once the parent shell know the directory to go to
#it can just cd itself.
DIR=`bash -c "cd ~$1;pwd"`
echo Changing to: $DIR
cd $DIR
pwd

-----------------------------------------------------------
Both of these approaches work on my machine (Mandrake 7.2 running bash-2.04-12.

HTH

--Steve
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 6428045
dir="~$1"
eval cd $dir
0
 
LVL 5

Expert Comment

by:paulqna
ID: 6428416
Putting the ~$1 like ~${1} could help. (ksh solution anyway)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 6429358
which ksh does this do?
0
 

Author Comment

by:titchener
ID: 6429613
Thanks, that did the job.
0
 
LVL 5

Expert Comment

by:paulqna
ID: 6429929
/bin/ksh ;)

No comment...
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6433246
paulqna, I meant the version, like pdksh, (AT&T) ksh
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
shell script help 1 156
Trying to install Node.js on Linux instance. Erroring.... 2 79
linux $PWD 2 686
What is the timeframe of this cron job? 2 147
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question